The Silent Failure of Deprecated Functions and Single-Row Queries
The error mysql_fetch_array() expects parameter 1 to be resource, boolean given is a classic symptom in PHP development, but it rarely stems from the logic of your SQL query itself. In the scenario where you execute a SELECT statement on a table with a primary key like ID, expecting exactly one row back, the root cause is almost exclusively the use of deprecated or removed database extension functions.
The code snippet mysql_fetch_array($query) attempts to pull data from a resource handle returned by an older MySQL API. If $query contains a boolean value instead of a resource, it means the underlying function used to execute the queryâlikely mysql_query() or mysql_select_db()âhas already been removed from standard PHP distributions since version 5.5. When you run this code in a modern environment (PHP 7.4+ or 8.x), the old function simply does not exist, and the interpreter fails before it can even evaluate your boolean check.
To solve this, you must migrate to mysqli or PDO, which are actively maintained and secure. Let's look at how to correctly fetch a single row when you know the query returns exactly one result set.
Executing the Query with Modern Drivers
Assuming you have migrated to MySQLi (Object-Oriented style), your workflow changes slightly but remains logically sound. You execute the query, check for success, and then fetch the specific row.
$connection = new mysqli("localhost", "user", "pass", "database");
// Prepare the statement or use a safe string interpolation depending on context
$sql = "SELECT * FROM friendzone WHERE ID = ?";
$stmt = $connection->prepare($sql);
$editID = $_GET['editID']; // Or whatever source your ID comes from
$stmt->bind_param("i", $editID);
$stmt->execute();
// This returns a mysqli_result object if successful, or false on failure
$result = $stmt->get_result();
if ($result && $row = $result->fetch_assoc()) {
// Access column values directly as strings or integers
echo $row['ID'];
} else {
// Handle cases where the row might not exist (NULL ID)
// Or handle connection/query errors if $result is false
if (!$result && !$connection->connect_error) {
echo "Query failed: " . $connection->error;
} else {
echo "No record found for this ID.";
}
}
$stmt->close();
Why the fetch_all Method Exists If You Only Need One Row
You might wonder why we use a fetch method at all if we don't intend to loop through multiple results. The confusion often arises from the distinction between fetching all rows versus fetching a specific row. Functions like fetch_assoc() or fetch_object() retrieve exactly one row and return it as an associative array or object, respectively. They do not require a while loop unless your dataset actually contains multiple matching records.
The logic holds: if you query by a unique primary key (WHERE ID = $editID), the result set will contain zero rows (if the record doesn't exist) or one row (if it does). You simply call fetch_assoc() once. If that method returns false, the record was not found.
Handling the "Boolean" Error in Legacy Code
If you are forced to maintain legacy code using the old mysql_* functions, the error occurs because modern PHP versions have stripped these functions entirely. There is no way to "fix" the boolean error without updating the library call. The migration path is mandatory for security and stability.
For developers working within the Laravel ecosystem, this problem is abstracted away via Eloquent ORM or the Query Builder, but the underlying principle remains: you are still dealing with a single resource handle that must be validated before attempting to fetch data.
Best Practices for Single-Row Access
When designing your application logic, avoid assuming a row exists without checking. Even though ID is unique, race conditions, concurrent deletions, or typos in the input can lead to empty result sets. Always validate the return value of your fetch method before accessing keys like $row['column_name']. Accessing an undefined index on a non-existent row will trigger a PHP warning that can expose sensitive logic flaws during debugging.
By switching to mysqli and using fetch_assoc(), you ensure compatibility with current PHP standards while maintaining the ability to handle single-row results efficiently without unnecessary overhead.
