Backend Development 4 min read

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, INSERT, UPDATE, and DELETE statements, and process results, including a complete example code snippet demonstrating connection, query execution, result handling, and cleanup.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP mysqli_query to Execute MySQL Queries

MySQL is a widely used relational database management system, and when developing web applications it is common to perform various query operations. PHP, as a popular server‑side language, provides many functions for connecting to and manipulating MySQL databases, among which the mysqli_query function is a frequently used method for executing queries.

The mysqli_query function can run different types of MySQL statements, including SELECT, INSERT, UPDATE, and DELETE. It accepts two arguments: the first is a database connection object, and the second is the SQL query string. Below is a sample code that uses mysqli_query to perform a SELECT query.

';
}

// Free result set
mysqli_free_result($result);

// Close connection
mysqli_close($connection);
?>

The code first creates a database connection and verifies it. It then runs a SELECT statement with mysqli_query , storing the result in the $result variable. A while loop iterates over $result , printing each row's ID, name, and age. Finally, the result set is freed and the connection closed.

Beyond SELECT, mysqli_query can execute other query types such as INSERT to add records, UPDATE to modify them, and DELETE to remove them—simply pass the appropriate SQL as the second argument.

It is important to note that mysqli_query returns a result set object for SELECT queries. You can retrieve rows using functions like mysqli_fetch_assoc , mysqli_fetch_row , or mysqli_fetch_array , depending on the desired format.

In summary, using PHP's mysqli_query function to perform MySQL operations is convenient and efficient; whether executing SELECT, INSERT, UPDATE, or DELETE statements, providing the correct SQL allows easy interaction with the database.

Additional links in the original source provide downloadable learning materials for Java, C, C++, front‑end, and PHP, but they are promotional and not part of the technical tutorial.

DatabaseMySQLQuerymysqli
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.