PHP Slim框架:使用PDO时出现细长的应用程序错误

时间:2017-06-09 12:06:21

标签: pdo slim

我想在我的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");

1 个答案:

答案 0 :(得分:0)

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