[英]Bootstrap Calendar - connect to database
我为我的应用程序使用了这个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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.