mysql_fetch_array() expects parameter 1 to be resource problem

mysql_fetch_array() expects parameter 1 to be resource problem

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

mysql_fetch_array() expects parameter 1 to be resource: The mysql_ Extension Trap

You are encountering a fatal mismatch between your PHP code and the state of the database extension you are using. The error message mysql_fetch_array() expects parameter 1 to be resource, boolean given is not a mystery; it is a direct result of using an obsolete API that has been deprecated and removed in modern PHP versions.

The root cause lies in the function call $con = mysql_connect(...). This belongs to the legacy mysql_* extension, which was officially deprecated in PHP 5.5 and completely removed in PHP 7.0. When you run this code on any standard server today (PHP 7, 8, or higher), the mysql_connect() function does not exist, or if it somehow executes via a legacy shim, it returns false (a boolean) rather than a resource handle because the underlying connection logic has changed or been disabled.

Consequently, when you pass this boolean value to mysql_fetch_array(), PHP throws the warning. You cannot fix this by simply changing the variable name or tweaking the SQL query; the entire API layer is incompatible with current environments.

The Solution: Migrate to PDO or MySQLi

To resolve this error and ensure your application runs securely, you must migrate away from the mysql_* functions. There are two primary paths forward: MySQLi (MySQL Improved) or PDO (PHP Data Objects).

Option 1: Switching to MySQLi

If you want to keep your connection style similar but with active support, switch to mysqli_connect(). This function returns a mysqli object (which acts as the resource), allowing fetch_array() to work correctly.

// Legacy code causing error:
// $con = mysql_connect("localhost","root","password");

// Updated MySQLi code:
$con = mysqli_connect("localhost", "root", "nitoryolai123$%^", "school");

if (!$con) {
    die('Could not connect: ' . mysqli_connect_error());
}

$result = mysqli_query($con, "SELECT * FROM student WHERE IDNO=".$_GET['id']);

while ($row = mysqli_fetch_array($result)) {
    // Now $row contains the data correctly.
}

Option 2: Switching to PDO (Recommended)

For new development or significant refactoring, PDO is generally preferred. It offers a unified interface for multiple database types and separates SQL logic from the connection layer more cleanly. Additionally, PDO allows you to use Prepared Statements, which are essential for preventing SQL injection vulnerabilities.

Your current code contains WHERE IDNO=".$_GET['id']. Directly embedding user input into a query string is a critical security flaw known as an SQL injection attack. With PDO, you can solve the connection issue and secure your application simultaneously:

try {
    $pdo = new PDO("mysql:host=localhost;dbname=school", "root", "nitoryolai123$%^");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Secure query using prepared statements
    $stmt = $pdo->prepare("SELECT YEAR FROM student WHERE IDNO = :id");
    $stmt->execute(['id' => $_GET['id']]);
    $result = $stmt; // $result is a PDOStatement object

    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        echo $row['YEAR'];
    }
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

Why the Legacy Code Fails Everywhere Else

Beyond the specific error, the mysql_* functions lack modern security features and are not maintained. If you are maintaining an older project running on a very old PHP version (5.6 or below), the error might be different (perhaps related to deprecated warnings). However, if you are seeing this error, it implies either:

  1. You are running on PHP 7+ where the extension is gone.
  2. The configuration file (php.ini) has mysql.default_port or similar legacy settings disabled.
  3. The mysqli or pdo_mysql extensions are not loaded properly.

Final Steps for Implementation

Before deploying your fix, ensure your server extensions are enabled. If you are using cPanel or a shared hosting environment, check the "Select PHP Version" tool to ensure you aren't accidentally selecting an ancient PHP version (like 5.x) unless specifically required for legacy compatibility.

For modern web applications, especially those handling sensitive data like student records, adopting PDO is the industry standard. It provides better error handling and a more robust architecture. If you are looking for resources on how to migrate specific projects or best practices for database management in Laravel ecosystems, https://laravelcompany.com offers documentation that aligns with these modern standards.

Stop using the mysql_* functions. Update your connection logic immediately to use either mysqli or PDO, and refactor your SQL queries to use prepared statements. This single change will eliminate the "resource, boolean given" error and secure your application against injection attacks.

Our network