PHP
MySQL
Web Development
Database
Error Handling

mysql_fetch_array/mysql_fetch_assoc/mysql_fetch_row/mysql_num_rows etc... expects parameter 1 to be resource

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Understanding MySQL Functions and the "Expects Parameter 1 to be Resource" Error

When working with MySQL in PHP, the mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_row(), and mysql_num_rows() functions are commonly used for fetching data from result sets. However, developers often encounter the error "expects parameter 1 to be resource" when using these legacy functions. Understanding why this error occurs and how to resolve it is crucial for efficient database handling in PHP.

The Functions Explained

Here's a brief explanation of each function and how they are typically used:

  • mysql_fetch_array(): This function fetches a result row as an associative array, a numeric array, or both. The syntax is as follows:
php
  mysql_fetch_array($result, $array_type = MYSQL_BOTH);

The function returns the next row of the result set or FALSE if there are no more rows.

  • mysql_fetch_assoc(): Similar to mysql_fetch_array(), this function only returns an associative array where the keys are the column names.
php
  mysql_fetch_assoc($result);
  • mysql_fetch_row(): This function returns a numerically indexed array, corresponding to the fetched row.
php
  mysql_fetch_row($result);
  • mysql_num_rows(): This function will return the number of rows in a result set.
php
  mysql_num_rows($result);

Each of these functions expects a valid resource, specifically a result resource generated by a mysql_query() call.

Understanding the "Expects Parameter 1 to be Resource" Error

Why It Occurs

The error "expects parameter 1 to be resource" occurs when the input parameter is not a valid resource. This typically happens when:

  1. The mysql_query() function fails to execute the SQL query properly, returning FALSE instead of a resource.
  2. The variable assigned to store the result of the query is incorrectly used, potentially being used before assignment or as the result of a failed query.

Example of a Problematic Code

php
1<?php
2$conn = mysql_connect('localhost', 'username', 'password');
3mysql_select_db('database_name', $conn);
4
5$result = mysql_query("SELECT * FROM non_existent_table"); // Hypothetical bad query
6
7if (!$result) {
8    die('Invalid query: ' . mysql_error());
9}
10
11while ($row = mysql_fetch_assoc($result)) {
12    print_r($row);
13}
14?>

In this example, if the table non_existent_table does not exist, mysql_query() will return FALSE, and mysql_fetch_assoc() will trigger the error because it expects a valid resource.

Solutions

  1. Error Handling and Validation: Always check if the query execution was successful before proceeding.
php
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }
  1. Migrating to MySQLi or PDO: The mysql_* functions are deprecated in PHP 5.5.0 and removed in PHP 7.0.0. Consider migrating to MySQLi or PDO for better security and performance.
  2. Ensure the Resource is Valid: Before using functions that require a resource, validate the result of mysql_query().

Migrating from mysql_* Functions

To modernize the code, here’s how you might use MySQLi:

php
1<?php
2$conn = new mysqli('localhost', 'username', 'password', 'database_name');
3
4if ($conn->connect_error) {
5    die('Connection failed: ' . $conn->connect_error);
6}
7
8$result = $conn->query("SELECT * FROM users");
9
10if ($result === FALSE) {
11    die('Invalid query: ' . $conn->error);
12}
13
14while ($row = $result->fetch_assoc()) {
15    print_r($row);
16}
17
18$conn->close();
19?>

Summary Table

FunctionType of ResultReturn TypeError Potential Reason
mysql_fetch_array()Array (Assoc + Num)Array or FALSENon-resource parameter
mysql_fetch_assoc()Assoc ArrayArray or FALSENon-resource parameter
mysql_fetch_row()Numeric ArrayArray or FALSENon-resource parameter
mysql_num_rows()Number of RowsInteger or FALSENon-resource parameter

Conclusion

The functions mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_row(), and mysql_num_rows() are now obsolete, but understanding how their parameters work is still valuable. An error saying "expects parameter 1 to be resource" points towards a misuse of result handling. Migrating to MySQLi or PDO is recommended for contemporary PHP development, offering improved functionality and security.


Course illustration
Course illustration

All Rights Reserved.