mysql_fetch_array()期望参数1为资源null

时间:2013-06-11 13:18:45

标签: php mysql

我无法弄清楚为什么我一直收到这个错误。这是我的代码。我试过切换东西,没有运气。我从我的书中得到了代码,所以我希望它会起作用,但情况并非总是如此。

警告:mysql_fetch_array()要求参数1为资源,在第16行的C:\ xampp \ htdocs \ CIS224 \ Company_Cars.php中给出null

<!DOCTYPE hmtl PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>
<title>Create Database</title>
</head>

<body>
<?php
$SQLstring = "SELECT * FROM company_cars";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
echo "<table width ='100%' border='1'\n";
echo "<tr><th>License</th><th>Make</th><th>Model</th><th>Mileage</th><th>Year</th></tr>\n";
while ($Row = mysqli_fetch_array($result) !== FALSE) {
    echo "<tr><td>{$Row[0]}</td>";
    echo "<td>{$Row[1]}</td>";
    echo "<td>{$Row[2]}</td>";
    echo "<td align='right'>{$Row[3]}</td>";
    echo "<td>{$Row[4]}</td></tr>\n";
    }
echo "</table>\n";

?>
</body>
</html>

4 个答案:

答案 0 :(得分:2)

您似乎忘记了连接到数据库。尝试

$DBConnect = mysql_connect('host', 'user', 'pass');
$db = mysql_select_db('dbname');

您也在致电mysqli_fetch_array,它应该是mysql_fetch_array

while ($Row = mysqli_fetch_array($QueryResult) !== FALSE) {
//                 ^ remove      ^ also this!!!

除非您实际使用mysqli_*,否则查询调用错误。你也传递了错误的变量,结果是$QueryResult而不是$result

你真的应该放弃mysql_*并学习PDO或MySQLi。

答案 1 :(得分:0)

删除@中的mysql_query()并尝试..

答案 2 :(得分:0)

为什么在@中使用mysql_query它会返回true或false。

$QueryResult = mysql_query($SQLstring, $DBConnect);

删除@,就像我在代码中所做的那样。它会起作用。

您的mysql_fetch_array功能还有另一个问题:

while ($Row = mysql_fetch_array($QueryResult))

使用上述代码代替您的代码。

始终使用PDO Mysql

答案 3 :(得分:0)

更改while循环

  while ($Row = mysqli_fetch_array($QueryResult) !== FALSE) {

替换

  ($result) to ($QueryResult)