mysqli_fetch_array()期望参数1为mysqli_result,boolean

时间:2014-03-16 12:13:07

标签: php mysql

我已经阅读了关于此错误的大部分内容,但似乎没有任何内容对我有用,我已经在脚本中添加了几个版本的错误报告,但它们都只是停止,仅显示主结果表 我收到以下错误:

 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in locations.php on line 29
坦率地说,我被困了。

<?php
$pagetitle="Locations";
$menu="yes";
require 'header.php';
require 'dbvars.php';
require 'dafunc.php';

//set the referrer variable
$self=htmlentities($_SERVER['PHP_SELF']);

// Make a MySQL Connection
$con=mysqli_connect($sqlhost,$sqluser,$sqlpass,$sqldb);
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

//display the edit dialog if required
if(isset($_POST['edit']))
{   
    $id=$_POST['id'];
    $result = mysqli_query($con,"SELECT * FROM location WHERE id='$id'");
        /*  
        //Found this on another SO.com post and tried it.  It cleared the error, but the script didn't continue and returned nothing.
        if($result === FALSE) {
            die(mysql_error()); // TODO: better error handling
        }
        */
    echo "<form action=\"$self\" method=\"post\">";
    echo "<table border=\"0\" align=\"center\">";

    //this is the line that is giving me the trouble!
    while($row = mysqli_fetch_array($result))
      {
      echo "<tr><td>ID</td><td>" . $row['id'] . "</td></tr>";
      echo "<input name=\"id\" type=\"hidden\" value=\"" . $row['id'] ."\" />";
      echo "<tr><td>Location</td><td><input name=\"location\" type=\"text\" value=\"". $row['location'] ."\"/></td></tr>";
      }
    echo "</table>";
    echo "<input type=\"submit\" name=\"confirm\" id=\"confirm\" value=\"Confirm Changes\" />";
    echo "</form>";

}

//write the changes to the DB
if(isset($_POST['confirm']))
{

    echo "write the changes to the database!";
}

//delete a record
if(isset($_POST['delete']))
{

    echo "delete the record!";
}

$result = mysqli_query($con,"SELECT * FROM valid_locations");

echo "
<form action=\"$self\" method=\"post\">
<table>
<tr><th>ID</th><th>Location</th></tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<input type=\"hidden\" name=\"id\" id=\"" . $row['id'] . "\">";
  echo "<td>" . $row['location'] . "</td>";
  echo "<input type=\"hidden\" name=\"location\" id=\"" . $row['location'] . "\">";
  echo "<td><input type=\"submit\" name=\"edit\" id=\"edit\" value=\"Edit\" /></td>";
  echo "<td><input type=\"submit\" name=\"delete\" id=\"delete\" value=\"Delete\" /></td>";
  echo "</tr>";
  }
echo "</table>";
echo "</form>";

mysqli_close($con);
?> 
</body>
</html>

3 个答案:

答案 0 :(得分:0)

发生这种情况是因为您的查询失败了。即$ result现在是一个布尔值为FALSE的值。我的猜测是您的表位置不存在或$_POST['id']包含特殊字符(这就是您应该使用预准备语句的原因)。

尝试使用类似的东西来找出问题所在:

$result = mysqli_query($con,"SELECT * FROM location WHERE id='$id'");
if($result === false){
    throw new Exception(mysqli_error($con));
}

答案 1 :(得分:0)

答案 2 :(得分:0)

此警告会导致您的SQL查询出错,请从查询中删除'

$result = mysqli_query($con,"SELECT * FROM location WHERE id = $id ");