2013-10-04 54 views
-1

我想使用PHP mysql_fetch_assoc函數並將我的表記錄返回到html表中。我只有一個記錄表中,現在,當我運行這最終返回這個PHP腳本返回,如果我做的唯一記錄的值...不能讓mysql_fetch_assoc()工作,但可以打印關聯數組 - PHP

print_r(mysql_fetch_assoc($QueryResult)); 

...

Array ([field_date] => 2013-10-03 [field_time] => 00:00:17 [name] => Dave [message] => This is a message. [switch] => Y) 

但是,以下內容不會將值放入html表格行,我需要幫助。

$SQLstring = "SELECT * FROM `table` WHERE switch = 'Y'"; 

    // Run query and place in variable 
    $QueryResult = mysql_query($SQLstring, $DBConnect); 

    print_r(mysql_fetch_assoc($QueryResult)); 

    // Set the fetch command to the query recordset 
    $Row = mysql_fetch_assoc($QueryResult); 

    // Return records into a formatted table 
    echo "<table width='100%' border='1'>\n"; 
    echo "<tr><th>Date</th><th>Time</th><th>Name</th> 
     <th>Message</th><th>Switch</th></tr>\n"; 
    while ($Row = $result -> mysql_fetch_row($QueryResult)) { 
     echo "<tr><td>{$Row['field_date']}</td>"; 
     echo "<td>{$Row['field_time']}</td>"; 
     echo "<td>{$Row['name']}</td>"; 
     echo "<td>{$Row['message']}</td>"; 
     echo "<td>{$Row['switch']}</td></tr>"; 
    } 
    echo "</table>\n"; 
+1

僅供參考MySQL擴展已被棄用,不再開發或支持。請切換到MySQLi或PDO擴展 – Phil

+0

謝謝,絕對值得注意。 – codingManiac

回答

0
while($Row = mysql_fetch_array($QueryResult)){ 
      print_r($Row); 
      echo "<br />"; 
} 
1

試試這個

$SQLstring = "SELECT * FROM `table` WHERE switch = 'Y'"; 

    // Run query and place in variable 
    $QueryResult = mysql_query($SQLstring, $DBConnect); 



    // Return records into a formatted table 
    echo "<table width='100%' border='1'>\n"; 
    echo "<tr><th>Date</th><th>Time</th><th>Name</th> 
     <th>Message</th><th>Switch</th></tr>\n"; 
    while ($Row = mysql_fetch_assoc($QueryResult)) { 
     echo "<tr><td>{$Row['field_date']}</td>"; 
     echo "<td>{$Row['field_time']}</td>"; 
     echo "<td>{$Row['name']}</td>"; 
     echo "<td>{$Row['message']}</td>"; 
     echo "<td>{$Row['switch']}</td></tr>"; 
    } 
    echo "</table>\n"; 
+0

我無法看到您在此處所做的更改。 – codingManiac

+1

@tripleddev Dilantha刪除了兩個對'mysql_fetch_assoc'的調用。每次調用它時,結果集遊標都是高級的。由於您只有一行,您只需要調用一次。他還刪除了你的糟糕的'$ Row = $ result->'語法錯誤 – Phil

+0

我在開始時刪除了print_r和fetch_assoc並更改了while循環 – Dilantha