2014-02-23 50 views
0

我已經使用PHP甘特類從GitHub這裏:MySQL的SELECT語句作爲PHP數組

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 「},{」label「:」hellooo「,」start「:」2012-07-22「,」end「:」2012-09-05「},{」label「:」hello 2「,」start「 :「2012-05-11」,「end」:「2012-06-03」}]

致命錯誤:無法將類型爲stdClass的對象用作/ home/inse1d/public_html/gantti-master/lib中的數組/gantti.php 44行

回答

0

您必須按照

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

然後

$json_response = json_encode($arr); 
+0

感謝,這爲我工作。 – Simon