PHP Slim Framework:使用PDO时的细长应用程序错误
问题描述:
我想用我的Slim php应用程序使用PDO
。当我使用简单的选择查询并发送json
数据到Twig page
。不过,我不断收到此错误:Slim Application Error
PHP Slim Framework:使用PDO时的细长应用程序错误
这是我的代码:
<?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");
答
问题解决了。它是由绑定参数引起的
消息:无法通过参考传递参数2
通过了解错误我已修复它。谢谢你们。
启用'debug'来查看实际的错误? –
@ ivanka-todorova我已经完成并修复了错误,非常感谢你 – Charaf