mysql_fetch_array()/ mysql_fetch_assoc()/ mysql_fetch_row()/ mysql_num_rows 等... 期望参数 1 为资源或结果

我试图从 MySQL 表中选择数据,但收到以下错误消息之一:

mysql_fetch_array()期望参数 1 为资源,给定布尔值

要么

mysqli_fetch_array()期望参数 1 为 mysqli_result,给定布尔值

要么

在布尔值 / 非对象上调用成员函数 fetch_array()

这是我的代码:

$username = $_POST['username'];
$password = $_POST['password'];

$result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');

while($row = mysql_fetch_array($result)) {
    echo $row['FirstName'];
}

同样适用于类似

$result = mysqli_query($mysqli, 'SELECT ...');
// mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
while( $row=mysqli_fetch_array($result) ) {
    ...

$result = $mysqli->query($mysqli, 'SELECT ...');
// Call to a member function fetch_assoc() on a non-object
while( $row=$result->fetch_assoc($result) ) {
    ...

$result = $pdo->query('SELECT ...', PDO::FETCH_ASSOC);
// Invalid argument supplied for foreach()
foreach( $result as $row ) {
    ...

$stmt = $mysqli->prepare('SELECT ...');
// Call to a member function bind_param() on a non-object
$stmt->bind_param(...);

$stmt = $pdo->prepare('SELECT ...');
// Call to a member function bindParam() on a non-object
$stmt->bindParam(...);

答案

$username = mysql_real_escape_string($_POST['username']);
$password = $_POST['password'];
$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'");

if($result === FALSE) { 
    die(mysql_error()); // TODO: better error handling
}

while($row = mysql_fetch_array($result))
{
    echo $row['FirstName'];
}
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
$result = mysqli_query($mysqli, "SELECT * FROM Users WHERE UserName LIKE '$username'");

// mysqli_query returns false if something went wrong with the query
if($result === FALSE) { 
    yourErrorHandler(mysqli_error($mysqli));
}
else {
    // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach
    foreach( $result as $row ) {
        ...
$username = $mysqli->escape_string($_POST['username']);
$result = $mysqli->query("SELECT * FROM Users WHERE UserName LIKE '$username'");

if($result === FALSE) { 
    yourErrorHandler($mysqli->error); // or $mysqli->error_list
}
else {
    // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach
    foreach( $result as $row ) {
      ...
$stmt = $mysqli->prepare('SELECT * FROM Users WHERE UserName LIKE ?');
if ( !$stmt ) {
    yourErrorHandler($mysqli->error); // or $mysqli->error_list
}
else if ( !$stmt->bind_param('s', $_POST['username']) ) {
    yourErrorHandler($stmt->error); // or $stmt->error_list
}
else if ( !$stmt->execute() ) {
    yourErrorHandler($stmt->error); // or $stmt->error_list
}
else {
    $result = $stmt->get_result();
    // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach
    foreach( $result as $row ) {
      ...

当查询中出现错误导致失败时,将显示此错误消息。使用时将显示出来:

  • mysql_fetch_array / mysqli_fetch_array()
  • mysql_fetch_assoc() / mysqli_fetch_assoc()
  • mysql_num_rows() / mysqli_num_rows()

注意 :如果您的查询没有行影响,则不会出现此错误。只有语法无效的查询才会生成此错误。

故障排除步骤

  • 确保已将开发服务器配置为显示所有错误。您可以通过将其放在文件顶部或配置文件中来做到这一点: error_reporting(-1); 。如果您有任何语法错误,这将为您指出。

  • 使用mysql_error()mysql_error()将报告 MySQL 在执行查询时遇到的任何错误。

    用法示例:

    mysql_connect($host, $username, $password) or die("cannot connect"); 
    mysql_select_db($db_name) or die("cannot select DB");
    
    $sql = "SELECT * FROM table_name";
    $result = mysql_query($sql);
    
    if (false === $result) {
        echo mysql_error();
    }
  • 从 MySQL 命令行或类似phpMyAdmin的工具运行查询。如果查询中存在语法错误,这将告诉您它是什么。

  • 确保您的报价正确。查询周围缺少引号或值可能会导致查询失败。

  • 确保您逃避了自己的价值观。查询中的引号可能会导致查询失败(也使您不愿接受 SQL 注入)。使用mysql_real_escape_string()转义您的输入。

  • 确保您没有混合使用mysqli_*mysql_*函数。它们不是同一件事,不能一起使用。 (如果要使用mysqli_*选择一个或另一个棍子,请参阅下面的原因。)

其他技巧

mysql_*函数不应用于新代码。它们已不再维护,社区已开始弃用过程 。相反,您应该了解准备好的语句,并使用PDOMySQLi 。如果您不确定, 本文将帮助选择。如果您想学习,这里有个不错的 PDO 教程

mysql_query("
SELECT * FROM Users 
WHERE UserName 
LIKE '".mysql_real_escape_string ($username)."'
");
$username = $_POST['username'];
$password = $_POST['password'];

$result = mysql_query("
SELECT * FROM Users 
WHERE UserName LIKE '".mysql_real_escape_string($username)."'
");

if($result)
{
    while($row = mysql_fetch_array($result))
    {
        echo $row['FirstName'];
    }
} else {
    echo 'Invalid query: ' . mysql_error() . "\n";
    echo 'Whole query: ' . $query; 
}
$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'");
mysql_select_db('database name ')or DIE('Database name is not available!');
$result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');

f($result === FALSE) {
    die(mysql_error());
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM Users WHERE UserName LIKE '$username'";
echo $query;
$result = mysql_query($query);

if($result === FALSE) {
    die(mysql_error("error message for the user")); 
}

while($row = mysql_fetch_array($result))
{
    echo $row['FirstName'];
}
$result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');

if( $result === FALSE ) {
   trigger_error('Query failed returning error: '. mysql_error(),E_USER_ERROR);
} else {
   while( $row = mysql_fetch_array($result) ) {
      echo $row['username'];
   }
}
$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '%$username%'");
while($row = mysql_fetch_array($result))
{
    echo $row['FirstName'];
}
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '%$username%'") or die(mysql_error());

while($row = mysql_fetch_array($result))
{
    echo $row['FirstName'];
}