Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法让mysql\u fetch\u assoc()工作,但可以打印关联数组——PHP_Php_Mysql - Fatal编程技术网

无法让mysql\u fetch\u assoc()工作,但可以打印关联数组——PHP

无法让mysql\u fetch\u assoc()工作,但可以打印关联数组——PHP,php,mysql,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 me

我试图使用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";
试试这个

$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";

仅供参考,MySQL扩展已弃用,不再开发或支持。请切换到MySQLi或PDO扩展谢谢,一定要小心。我看不出您在这里做了什么更改。@tripleddev Dilantha删除了对MySQLi_fetch_assoc的两个调用。每次调用它时,结果集游标都是高级的。因为您只有一行,所以只想调用它一次。他还删除了你古怪的$Row=$result->语法错误我删除了你的打印和获取关联,并更改了while循环好的,我尝试了,现在它为每个记录生成空单元格,但没有显示任何数据。
$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";