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

Mysql_fetch_array()期望参数1为给定的资源布尔值

这是我的代码:

$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是否失败

$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');
if(!$result)
{
     echo "error executing query: "+mysql_error(); 
}else{
       while($row = mysql_fetch_array($result))
       {
         echo $row['FirstName'];
       }
}

其他回答

如果检查时没有出现任何MySQL错误,请确保正确创建了数据库表。这发生在我身上。寻找任何不需要的逗号或引号。

如果数据库未选中,请检查,因为有时数据库未选中

检查

mysql_select_db('database name ')or DIE('Database name is not available!');

MySQL查询前 然后进入下一步

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

f($result === FALSE) {
    die(mysql_error());
$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'];
}

有时将查询抑制为@mysql_query(您的查询);

试试这个,它必须工作,否则您需要打印错误来指定您的问题

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

$sql = "SELECT * from Users WHERE UserName LIKE '$username'";
$result = mysql_query($sql,$con);

while($row = mysql_fetch_array($result))
{
    echo $row['FirstName'];
}
<?php
    $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'];
    }
?>

如果有一个用户具有唯一的用户名,您可以使用“=”表示。没有必要喜欢。

您的查询将是:

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