无法使mysql_fetch_assoc()工作,但可以打印关联数组 - PHP

时间:2013-10-04 04:13:14

标签: php mysql

我正在尝试使用PHP mysql_fetch_assoc函数并将我的表记录返回到html表中。我现在只在表中有一条记录,当我运行这个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";

2 个答案:

答案 0 :(得分: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";

答案 1 :(得分:0)

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