我试图从一个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'];
}

当前回答

不要使用已废弃的mysql_*函数(在php 5.5中已废弃的将在php 7中被删除),你可以使用mysqli或pdo来实现

下面是完整的选择查询

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        // code here 
    }
} else {
    echo "0 results";
}
$conn->close();
?>

其他回答

查询可能会由于各种原因而失败,在这种情况下,mysql_*和mysqli扩展都会从各自的查询函数/方法中返回false。您需要测试该错误条件并相应地处理它。

mysql_扩展:

mysql_函数已弃用,在php版本7中已被删除。

在将$result传递给mysql_fetch_array之前检查$result。您会发现它是假的,因为查询失败了。请参阅[mysql_query][1]文档了解可能的返回值以及如何处理它们的建议。

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

if($result === FALSE) { 
    trigger_error(mysql_error(), E_USER_ERROR);
}

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

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

mysql_fetch_array / mysqli_fetch_array () 作用是()/ mysqli_fetch_assoc () mysql_num_rows () / mysqli_num_rows ()

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

故障排除步骤

Make sure you have your development server configured to display all errors. You can do this by placing this at the top of your files or in your config file: error_reporting(-1);. If you have any syntax errors this will point them out to you. Use mysql_error(). mysql_error() will report any errors MySQL encountered while performing your query. Sample usage: 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(); } Run your query from the MySQL command line or a tool like phpMyAdmin. If you have a syntax error in your query this will tell you what it is. Make sure your quotes are correct. A missing quote around the query or a value can cause a query to fail. Make sure you are escaping your values. Quotes in your query can cause a query to fail (and also leave you open to SQL injections). Use mysql_real_escape_string() to escape your input. Make sure you are not mixing mysqli_* and mysql_* functions. They are not the same thing and cannot be used together. (If you're going to choose one or the other stick with mysqli_*. See below for why.)

其他技巧

Mysql_ *函数不应该用于新代码。它们不再被维护,社区已经开始了弃用过程。相反,你应该学习准备语句并使用PDO或MySQLi。如果你不能决定,这篇文章将帮助你选择。如果你想学习,这里有一个很好的PDO教程。

试试这个

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

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

在To之前使用db_close()确保你没有关闭数据库 运行查询:

如果你在一个脚本中使用多个查询,即使你包含了包含查询或数据库连接的其他页面,那么在你使用db_close()的任何地方都可能关闭你的数据库连接,所以确保你没有在你的脚本中犯这个错误。

传统上,PHP对代码中的错误实践和失败是宽容的, 这使得调试非常困难。 在这个特定的情况下,问题是mysqli和PDO 默认情况下,查询失败时不告诉你,只返回FALSE。 (我不会谈论被废弃的mysql扩展。 对预处理语句的支持是切换到PDO或mysqli的充分理由。) 但是您可以更改PHP的默认行为,在查询失败时总是抛出异常。

对于PDO:使用$ PDO ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

error_reporting(E_ALL);

$pdo = new PDO("mysql:host=localhost;dbname=test", "test","");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$result = $pdo->query('select emal from users');
$data = $result->fetchAll();

这将显示以下内容:

致命错误:未捕获的异常'PDOException'与消息'SQLSTATE[42S22]:列未找到:1054未知列'emal'在'字段列表"在E:\htdocs\test\mysql_errors\pdo.php第8行 PDOException: SQLSTATE[42S22]:列未找到:1054在E:\htdocs\test\mysql_errors\pdo.php第8行“字段列表”中未知列“emal

如您所见,它准确地告诉您查询有什么问题,以及在代码中的哪里修复它。

没有$pdo->setAttribute(pdo::ATTR_ERRMODE, pdo::ERRMODE_EXCEPTION); 你会得到

致命错误:在E:\htdocs\test\mysql_errors\pdo.php第9行调用成员函数fetchAll(

mysqli:使用mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

error_reporting(E_ALL);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'test', '', 'test');

$result = $mysqli->query('select emal from users');
$data = $result->fetch_all();

你会得到

致命错误:未捕获的异常'mysqli_sql_exception'与消息'未知列'emal'在'字段列表"在E:\htdocs\test\mysql_errors\mysqli.php第8行 mysqli_sql_exception:在E:\htdocs\test\mysql_errors\mysqli.php第8行“字段列表”中的未知列“emal

没有MYSQLI_REPORT_ERROR (MYSQLI_REPORT_STRICT);你只能得到

致命错误:在E:\htdocs\test\mysql_errors\mysqli.php的第10行调用成员函数fetch_all(

当然,您可以手动检查MySQL错误。 但如果每次我打错字都要这么做,我会疯掉的 或者更糟——每次我想查询数据库的时候。