mysql select语句作为php数组

时间:2014-02-23 00:34:28

标签: php mysql

我在这里使用了Github的PHP Gantt类:

https://github.com/bastianallgeier/gantti

我有一个php脚本,可以从php数组中的数据生成甘特呗:

$data = array();
$data[] = array(
  'label' => 'Project 1',
  'start' => '2012-04-20', 
   'end'   => '2012-05-12'
);
$data[] = array(
  'label' => 'Project 2',
  'start' => '2012-04-22', 
  'end'   => '2012-05-22'
);

而不是我想使用mysql数据库将结果作为数组打印出来,其布局与之前加载数据的方式相匹配:

$query=mysql_query("select main_task AS 'label', start, end from tasks") or
die(mysql_error());
// Collect the results
while($obj = mysql_fetch_object($query)) {
$arr[] = $obj;
}

// JSON-encode the response
$json_response = json_encode($arr);

// Return the response
echo $json_response;

这是我得到的错误回复:

[{ “标签”: “ARIS”, “启动”: “2012-05-15”, “结束”: “2012-07-03”},{ “标记”: “测试”, “开始” : “2012-06-01”, “端”: “2012-07-03”},{ “标记”: “测试1”, “启动”: “2012-06-01”, “端”:“2012- 08-05 “},{” 标记 “:” 亚当斯 “ ”启动“: ”2012-05-06“, ”端“: ”2012-06-17“},{ ”标记“: ”hellooo“,”开始“:”2012-07-22“,”结束“:”2012-09-05“},{”label“:”你好2“,”开始“:”2012-05-11“,”结束“: “2012-06-03”}]

致命错误:不能在第44行的/home/inse1d/public_html/gantti-master/lib/gantti.php中使用stdClass类型的对象作为数组

1 个答案:

答案 0 :(得分:0)

你必须这样做

while($obj = mysql_fetch_object($query)) {
  $arr[] = array('label'=>$obj->label,'start'=>$obj->start,'end'=>$obj->end);
}

然后

$json_response = json_encode($arr);