mysql_fetch_array() expects parameter 1 to be resource
It looks like you are trying to use a function from the MySQL extension in PHP, but you are getting an error saying that the first parameter should be a resource.
It looks like you are trying to use a function from the MySQL extension in PHP, but you are getting an error saying that the first parameter should be a resource.
This error is usually caused by one of the following issues:
- You are trying to use the function on a variable that is not a valid MySQL result resource. Make sure that you have a valid MySQL result resource before calling any of these functions.
- You are using an outdated version of PHP. The MySQL extension has been deprecated as of PHP 7.0 and has been removed as of PHP 7.4. If you are using a version of PHP later than 7.0, you should use either the MySQLi extension or PDO instead.
- You are passing an incorrect variable type (e.g.,
null,false, or the connection resource instead of the result resource) to the function. This often happens when a query fails and returnsfalse, but the failure isn't checked before fetching.
To fix the problem, you should check your code and make sure that you are using a valid MySQL result resource and that you are using the correct function for the version of PHP that you are using.
Before (Error):
// Fails if query returns false or $result is not a resource
$result = mysql_query("SELECT * FROM users");
$row = mysql_fetch_array($result); // Warning: expects parameter 1 to be resourceAfter (Fixed with MySQLi & Error Handling):
$conn = mysqli_connect("localhost", "username", "password", "database");
$result = mysqli_query($conn, "SELECT * FROM users");
if ($result) {
$row = mysqli_fetch_array($result);
} else {
echo "Query failed: " . mysqli_error($conn);
}Migration to PDO:
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
$stmt = $pdo->query('SELECT * FROM users');
$row = $stmt->fetch(PDO::FETCH_ASSOC);