Bootstrap Calendar - 连接数据库

时间:2014-08-07 22:47:23

标签: php mysql twitter-bootstrap calendar

我将这个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;

基本上,我的日程表中没有任何结果。

感谢。

1 个答案:

答案 0 :(得分:3)

这是因为$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;