How can the error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" be resolved when fetching data from a MySQL query in PHP?
The error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" occurs when the result of a MySQL query is not successfully returned. This can happen due to errors in the query itself or connection issues. To resolve this issue, ensure that the query is executed correctly and that the connection to the MySQL database is established.
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Execute query
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);
// Check if query executed successfully
if($result) {
// Fetch data from query result
while($row = mysqli_fetch_array($result)) {
// Process data
}
} else {
echo "Error: " . mysqli_error($connection);
}
// Close connection
mysqli_close($connection);
Keywords
Related Questions
- What best practices should be followed when filtering out certain files in a PHP script?
- What potential issue or pitfall is the user experiencing with their PHP code when attempting to insert data into a database?
- What is the best practice for loading a variable content into a textarea when the page is loaded in PHP?