Getting query results is a common and important step when interacting with MySQL using PHP. mysql_fetch_array() is a function used in PHP to obtain query results. It can return the results as an associative array, a numeric index array, or both, so that we can process them in subsequent programs.
Note : mysql_fetch_array() belongs to the mysql extension, which has been abandoned since PHP 7.0. It is recommended that new projects use mysqli or PDO_MySQL . This article is mainly aimed at maintaining old systems or learning purposes.
Before we start, we need to connect to the MySQL database and select a database:
<code> <?php // Connect to the database $conn = mysql_connect("localhost", "username", "password"); if (!$conn) {
die("Connection failed: " . mysql_error());
}
// Select database
mysql_select_db("test_db", $conn);
?>
</code>
Execute a simple SQL query to obtain all user information in the user table:
<code> <?php $sql = "SELECT id, name, email FROM users"; $result = mysql_query($sql, $conn); if (!$result) {
die("Query failed: " . mysql_error());
}
?>
</code>
Next, we use mysql_fetch_array() to get the query result:
<code> <?php while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "ID: " . $row["id"] . "<br>"; echo "Name: " . $row["name"] . "<br>"; echo "Email: " . $row["email"] . "<br>"; } ?> </code>In the above code, we pass in the MYSQL_ASSOC constant, which means we want to get the data in the way of associating the array. This means that we can access the value of each column by the field name.
The second parameter of mysql_fetch_array() can be set to the following modes:
MYSQL_ASSOC : Returns only the associative array.
MYSQL_NUM : Returns only the numeric index array.
MYSQL_BOTH (default): Returns both the associative and numeric index arrays.
Example (get association and index at the same time):
<code> <?php while ($row = mysql_fetch_array($result)) { echo $row[0]; // Use number index echo $row["name"]; // Use field name} ?> </code>Here is a complete usage process:
<code> <?php // Connect to the database $conn = mysql_connect("localhost", "root", "password"); if (!$conn) { die("Connection failed: " . mysql_error()); }mysql_select_db("test_db", $conn);
// Query data
$sql = "SELECT id, name, email FROM users";
$result = mysql_query($sql, $conn);
// Output result
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<p>";
echo "User ID: " . $row["id"] . "<br>";
echo "Username: " . $row["name"] . "<br>";
echo "User Email: " . $row["email"] . "<br>";
echo "Profile link: <a href=' https://gitbox.net/user/ " . $row["id"] . "'> View</a>";
echo "</p>";
}
// Close the connection
mysql_close($conn);
?>
</code>
mysql_fetch_array() provides a flexible way to get MySQL query results. While it is no longer recommended for use in new projects, understanding how it works will still help to understand PHP and database interactions. When maintaining an old system, it is necessary to master how to use it.
When developing new systems, it is recommended to use mysqli_fetch_assoc() or PDOStatement::fetch() instead. No matter which method is used, understanding the underlying query logic is always the basis for building reliable applications.