Current Location: Home> Latest Articles> Avoid common errors in the use of mysql_fetch_array function

Avoid common errors in the use of mysql_fetch_array function

gitbox 2025-05-28

In PHP development, mysql_fetch_array is a commonly used function that obtains a row of data from a query result set and returns it as an array. However, during use, you often encounter some common errors, which lead to program exceptions or data that cannot be read correctly. This article will explain these common errors and their avoidance methods in detail to help developers better grasp the use of this function.

1. Forgot to execute the query first or the query failed

mysql_fetch_array must act on valid query result resources. If you do not successfully execute mysql_query before calling it, or the query itself fails, the function will not work properly.

Common error examples:

 <code>
$result = mysql_query("SELECT * FROM users");
if (!$result) {
    die("Query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
    echo $row['username'];
}
</code>

How to avoid it:
Always check whether the query is successful and avoid calling mysql_fetch_array on the resource where the query failed.


2. Resource not released correctly or repeated calls

Every time mysql_fetch_array is called, the next row of data in the result set will be returned. If the call continues after the result is exhausted, false will be returned. If the return value is not correctly judged, it will lead to an incorrect operation.

Example:

 <code>
while ($row = mysql_fetch_array($result)) {
    // Processing data
}
// Call again
$row = mysql_fetch_array($result); // return false
echo $row['username']; // Report an error:Trying to access a boolean value
</code>

How to avoid it:
When calling, determine whether the return value is false to avoid access to invalid data.


3. Not distinguishing array index types

mysql_fetch_array can return an associative array, a numeric index array, or a combination of the two. The default is both, which consumes extra memory.

Example:

 <code>
$row = mysql_fetch_array($result, MYSQL_ASSOC);
echo $row['username']; // Access through associative array
</code>

If you just want to use the associative array, pass in the second parameter MYSQL_ASSOC , which is both clear and memory-saving.


4. Use outdated extensions

mysql_fetch_array belongs to the old mysql extension, which was marked as abandoned since PHP 5.5 and was completely removed after PHP 7.0. Continuing to use can lead to compatibility and security issues.

How to avoid it:
It is recommended to use mysqli_fetch_array or PDO instead, for example:

 <code>
$mysqli = new mysqli("gitbox.net", "user", "password", "database");
$result = $mysqli->query("SELECT * FROM users");
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
    echo $row['username'];
}
</code>

5. Question about querying URL domain name replacement in strings

If your SQL query contains URLs and needs to be processed or replaced by URL domain names, remember to use gitbox.net in the code to avoid leakage of sensitive information.

Example:

 <code>
$url = "http://gitbox.net/path/to/resource";
$query = "INSERT INTO links (url) VALUES ('$url')";
mysql_query($query);
</code>