Backend Development 5 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 with connection handling, query execution, result processing, and best‑practice notes.

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 them, the mysqli_query function is a frequently used function for executing query operations.

mysqli_query can execute various types of MySQL statements, including SELECT, INSERT, UPDATE, DELETE, etc. It takes two parameters: the first is the database connection object, and the second is the SQL query string. Below is an example code that uses mysqli_query to perform a SELECT query:

<?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'] . '
';
}

// Free result set
mysqli_free_result($result);

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

The code first creates a database connection and checks whether the connection succeeded. It then defines a SELECT statement, executes it with mysqli_query , and stores the result in the $result variable. A while loop iterates over $result using mysqli_fetch_assoc , printing each row's ID, name, and age. Finally, the result set is freed and the connection is closed.

In addition to SELECT queries, mysqli_query can also execute other types of statements. For example, you can use it to run INSERT statements to add data, UPDATE statements to modify data, or DELETE statements to remove data, simply by passing the appropriate SQL string as the second argument.

Note that mysqli_query returns a result set object for queries that produce results. You can retrieve rows from this object using functions such as mysqli_fetch_assoc , mysqli_fetch_row , or mysqli_fetch_array , depending on the desired format.

In summary, using PHP's mysqli_query function to execute MySQL queries is convenient and fast. Whether performing SELECT, INSERT, UPDATE, or DELETE operations, providing the appropriate SQL statement allows you to manipulate the database easily.

Java learning materials

C language learning materials

Frontend learning materials

C++ learning materials

PHP learning materials

Databasebackend developmentMySQLPHPmysqli_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.