2013-01-22 16 views
-1

對不起Q的輕微重疊,但我認爲現在以前的答案是貶值(我已經看到了,在PHP 5.3,你需要使用MySQLi_fetch_array())並使陣列

我使用jpgraph並試圖從MySQL數據庫中提取數據。我有下面的代碼,它拉出我需要的數字,當我回應結果時,它逐個吐出答案沒有問題,但它不會將它放入數組中。

我試圖取代線是

$ydata = array(1,2,3,4,5,6,7); 


//query 
$sql = "SELECT CONCAT('Week ', WEEK(TimeofCompletion) , ' ', YEAR(TimeofCompletion)) AS Week, Count(*) AS VolumeOfAnswers 
FROM table01 
GROUP BY WEEK(TimeofCompletion) , YEAR(TimeofCompletion) 
ORDER BY TimeofCompletion"; 

$ydata = array(); 

while($row = mysql_fetch_array($sql)){ 
// Create a temporary array 
$temp = array(); 

$temp[] = "".$row['VolumeOfAnswers'].""; 
$ydata = '['. implode(', ', $temp) . ']'; 
} 

任何建議,我怎麼會得到按行輸出到一個數組?

問候 Maudise

回答

0
$temp[] = "".$row['VolumeOfAnswers'].""; //what this suppose to do? 

我想,你希望我身邊的報價會增加內部

$temp[] = "'".$row['VolumeOfAnswers']."'"; 

單引號,否則你不需要雙引號在所有所以我會刪除它們。之後你的數組將保存來自數據庫的值。

根據documentationmysql_fetch_array已棄用,您應該使用PDO或MySQLi。但如果你仍然想使用它,我建議使用mysql_fetch_assoc來代替。

現在這樣一個問題:

while($row = mysql_fetch_array($sql)){ 
    // Create a temporary array 
    $temp = array(); 

    $temp[] = "".$row['VolumeOfAnswers'].""; 
    //why is this here? should be outside of while loop shouldnt it? 
    $ydata = '['. implode(', ', $temp) . ']'; 
} 

您$ YDATA總是被覆蓋,並擁有唯一的$臨時數組的最後一個元素。或者移動while循環之外的$ ydata或使用array_merge($ydata, $temp);

+0

[編輯添加爲答案] – Maudise

+0

嗨,由於某種原因,它告訴我數組是空的?我遇到的其他錯誤是它無法定義最小/最大值,這意味着我認爲它將它讀爲字符串變量而不是數字。有任何想法嗎? – Maudise