【问题标题】:PHP Slim Framework : Slim Application Error when using PDOPHP Slim 框架:使用 PDO 时出现 Slim 应用程序错误
【发布时间】:2017-06-09 12:06:20
【问题描述】:

我想在我的 Slim php 应用程序中使用PDO。当我使用简单的选择查询并将json 数据发送到Twig page 时。但我不断收到此错误:Slim Application Error

这是我的代码:

<?php
require __DIR__ . '/vendor/autoload.php';

$app = new Slim\App;

$container = $app->getContainer();

    $container['view'] = function ($container) {
    $templates = __DIR__ . '/templates/';
    $cache = __DIR__ . '/tmp/views/';
    $view = new Slim\Views\Twig($templates, array('cache' => false));
    return $view;
};

$container['db'] = function ($container) {

    $pdo = new PDO("mysql:host=localhost;DBName=dbsat", "root", "");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};


$app->get('/', function ($request, $response) {

    $sth = $this->db->prepare("SELECT * from client where id=:id");
    $sth->bindParam("id", 1);
    $sth->execute();
    $todos = json_encode($sth->fetchAll());

    $data = ['user' => $todos];
    return $this->view->render($response, 'home.twig', $data);
});


$app->get('/login', function ($request, $response) {
    return $this->view->render($response, 'login.twig');
});



$app->run();

?>

问题出现在这一行:

$sth = $this->db->prepare("SELECT * from client where id=:id");

【问题讨论】:

  • 启用debug 以查看实际错误?
  • @ivanka-todorova 我已经完成并修复了错误,非常感谢

标签: pdo slim


【解决方案1】:

问题解决了。它是由绑定参数引起的
消息:无法通过引用传递参数 2
通过知道错误,我已修复它。谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多