Using PHP mysqli_query to Execute MySQL Queries
This article explains how to use PHP's mysqli_query function to connect to a MySQL database, execute SELECT statements, process result sets with mysqli_fetch_assoc, and handle other query types such as INSERT, UPDATE, and DELETE, including full example code and best practices.
MySQL is a widely used relational database management system, and when developing web applications PHP provides functions to connect and operate MySQL databases. The mysqli_query function is a common way to execute queries.
The function can run SELECT, INSERT, UPDATE, DELETE and other SQL statements; it takes a database connection object and the SQL string as parameters.
';
}
// Free result set
mysqli_free_result($result);
// Close connection
mysqli_close($connection);
?>The script first creates and verifies a database connection, then runs a SELECT statement with mysqli_query , stores the result in $result , iterates over each row to output the ID, name, and age, and finally frees the result set and closes the connection.
Beyond SELECT, mysqli_query can execute INSERT, UPDATE, DELETE and other statements by passing the appropriate SQL string as the second argument.
When a query returns a result set, functions such as mysqli_fetch_assoc , mysqli_fetch_row , or mysqli_fetch_array can be used to retrieve rows in different formats.
In summary, using PHP's mysqli_query provides a convenient and efficient way to perform various MySQL operations within backend development.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.