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 perform MySQL operations such as SELECT, INSERT, UPDATE, and DELETE, providing a step‑by‑step example that creates a connection, runs a SELECT query, processes results, and handles errors 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 programming language, provides many functions to connect to and manipulate MySQL databases, among which the mysqli_query function is frequently used to execute queries.

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

<code><?php
// Create database connection
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

// Check connection
if (!$connection) {
    die('Database connection failed: ' . mysqli_connect_error());
}

// Execute SELECT query
$query = "SELECT id, name, age FROM users";
$result = mysqli_query($connection, $query);

// Check query success
if (!$result) {
    die('Query failed: ' . mysqli_error($connection));
}

// Process result set
while ($row = mysqli_fetch_assoc($result)) {
    echo 'ID: ' . $row['id'] . ', 姓名: ' . $row['name'] . ', 年龄: ' . $row['age'] . '<br>';
}

// Free result set
mysqli_free_result($result);

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

The code first creates a database connection and checks its success. It then executes a SELECT statement with mysqli_query , storing the result in the $result variable. By looping over $result , it prints each row’s ID, name, and age, and finally frees the result set and closes the connection.

Beyond SELECT, the mysqli_query function can also execute other statements such as INSERT to add data, UPDATE to modify data, and DELETE to remove data; you simply pass the appropriate SQL string as the second argument.

It is important to note that mysqli_query returns a result set object for SELECT queries. You can retrieve rows from this object 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 queries is convenient and efficient. Whether you need to run SELECT, INSERT, UPDATE, or DELETE operations, providing the correct SQL statement allows you to manipulate the database with ease.

BackendSQLDatabaseMySQLmysqli_query
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.