gpt4 book ai didi

php - Bootstrap 日历 - 连接到数据库

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:48 25 4
gpt4 key购买 nike

我用这个https://github.com/Serhioromano/bootstrap-calendar我的应用程序的日历,我收到此错误

PHP Notice:  Trying to get property of non-object in .../events.json.php on line 9

能否请您看一下,让我知道我做错了什么:

$db    = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
$sql = sprintf('SELECT * FROM table_events');

$out = array();
foreach($db->query($sql) as $row) {
$out[] = array(
'id' => $row->id,
'title' => $row->name,
'url' => $row->url,
'start' => strtotime($row->datetime) . '000',
'end' => strtotime($row->datetime_end) .'000'
);
}

echo json_encode(array('success' => 1, 'result' => $out));
exit;

基本上,我不会在我的日历中返回任何结果。

谢谢。

最佳答案

因为$db->query($sql)没有返回对象,你写$row->id,但一定是 $row["id"] 一个数组

如果你想要语法对象:

$db    = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'username', 'password');
$sql = 'SELECT * FROM events';

$res = $db->query($sql);
$res->setFetchMode(PDO::FETCH_OBJ);

$out = array();
foreach($res as $row)
{
$out[] = array(
'id' => $row->id,
'title' => $row->name,
'url' => $row->url,
'start' => strtotime($row->datetime) . '000',
'end' => strtotime($row->datetime_end) .'000'
);
}

echo json_encode(array('success' => 1, 'result' => $out));
exit;

关于php - Bootstrap 日历 - 连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25193473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com