2012-07-03 36 views
3

我很難組織從mysql_fetch_array()獲得的數據。需要幫助從mysql_fetch_array()中訂購數據

我有一個數據庫表看起來是這樣的:

+---------------------+------------------+---------------+--------+---------+ 
| date    | name    | indexed_pages | nameID | entryID | 
+---------------------+------------------+---------------+--------+---------+ 
| 2012-06-15 21:18:06 | site1.com  |   200 |  1 |  1 | 
| 2012-06-15 21:18:10 | site2.com  |   25 |  2 |  1 | 
| 2012-06-15 21:18:13 | site3.com  |   12 |  3 |  1 | 
| 2012-06-15 21:18:16 | site4.com  |    8 |  4 |  1 | 
| 2012-06-15 21:18:19 | site5.com  |    2 |  5 |  1 | 
| 2012-06-16 00:11:12 | site1.com  |   191 |  1 |  2 | 
| 2012-06-16 00:11:21 | site2.com  |   25 |  2 |  2 | 
| 2012-06-16 00:11:30 | site3.com  |   12 |  3 |  2 | 
| 2012-06-16 00:11:44 | site4.com  |    8 |  4 |  2 | 
| 2012-06-16 00:11:51 | site5.com  |    2 |  5 |  2 | 
| 2012-06-18 10:20:47 | site1.com  |   191 |  1 |  3 | 
| 2012-06-18 10:20:52 | site2.com  |   25 |  2 |  3 | 
| 2012-06-18 10:20:56 | site3.com  |   12 |  3 |  3 | 
| 2012-06-18 10:21:00 | site4.com  |    8 |  4 |  3 | 
| 2012-06-18 10:21:04 | site5.com  |    2 |  5 |  3 | 
+---------------------+------------------+---------------+--------+---------+ 

我需要爲了在谷歌線圖的結果通過以下方式:

['date', 'site1_entryID=1', 'site2_entryID=2', 'site3_entryID=3', (...)],"; 

的事情是,我無法管理我生成的陣列。我使用下面的代碼:

mysql_connect("host_here", "username_here", "pass_here") or die(mysql_error()); 
mysql_select_db("my_database") or die(mysql_error()); 
$query = "SELECT * FROM pages"; 
$result = mysql_query($query); 
$row = mysql_fetch_array($result); 

這個我需要呼應indexed_pa​​ges的數量爲每個網站,ENTRYID = 1

我不知道這是否說明是混亂與否之後,但我已經嘗試了幾乎所有的東西,無法從數組中獲取組織數據來服務我需要做的事情。請幫助!

在此先感謝!

回答

1

我認爲最簡單的查詢是:

$result= mysql_query("SELECT name, index_pages, entryID from table_name WHERE entryID =  
1"); 
while($row=mysql_fetch_array($result)){ 
    echo "$row[name]"; 
    echo "$row[index_pages]"; 
    echo "$row[entryID]"; 
} 

試試這個。可能會有一些錯誤。因爲我發展得很快。並用你的名字替換table_name。

或者你可以在一個表中顯示它:

echo "<table>"; 
echo "<tr><td>Sit Name</td>"; 
echo "<td>Page Name</td>"; 
echo "<td>EntryID</td>"; 
echo "</tr>"; 
while($row=mysql_fetch_array($result)){ 
    echo "<tr>"; 
    echo "<td>$row[name]</td>"; 
    echo "<td>$row[index_pages]</td>"; 
    echo "<td>$row[entryID]</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 
4

不要使用select *,這是懶了,你被困接受的DB決定生產它們的順序領域

指定所需的字段的順序你想要的:。

SELECT date, name, indexed_pages, etc... 
+0

嘿,馬克@-B。是的,我也這樣做了,但無濟於事。
我試過了:'SELECT date,name,indexed_pa​​ges,nameID,entryID FROM pages;'但仍然無法使用mysql_fetch_array()的數據輸出,因爲它以我無法處理的方式對它進行排序:S –

0
SELECT date, name, indexed_pages 
FROM pages 
where entryID=1 
order by date asc ,name asc 
0

不知道這將有助於

mysql_connect("host_here", "username_here", "pass_here") or die(mysql_error()); 
mysql_select_db("my_database") or die(mysql_error()); 
$query = "SELECT * FROM pages"; 
$result = mysql_query($query); 
$data[]='date'; 
while($row = mysql_fetch_assoc($result)){ 
    $name=substr($row['name'], -4); 
    $data[]= $name."_entryID=".$row['entryID']; 
} 

有點蠻力方法。