<?php
$db = new PDO('mysql:host=localhost:3306;dbname=DB1;', 'user1','123456');
$sql = 'SELECT * FROM events';
$out = array();
foreach($db->query($sql) as $row) {
$out[] = array(
'id' => $row->title,
'title' => $row->name,
'url' => $row->url,
'class' => $row->class,
'start' => $row->start . '000',
'end' => $row->end .'000'
);
}
echo json_encode(array('success' => 1, 'result' => $out));
exit;
?>
表格结构
Field Type Null Key Default Extra
id int(5) NO NULL
title text NO NULL
url text NO NULL
class text NO NULL
start datetime NO NULL
end datetime NO NULL
显示输出为
{"success":1,"result": [{"id":null,"title":null,"url":null,"class":null,"start":"000","end":"000"}, {"id":null,"title":null,"url":null,"class":null,"start":"000","end":"000"}]}
我想在表中打印数据而不是null
谢谢。
答案 0 :(得分:0)
答案 1 :(得分:0)
没试过,但你可以改变这个:
foreach($db->query($sql) as $row) {
到
foreach($db->query($sql) as (object)$row) {
...这可能允许用于访问记录的语法。