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 closes the database connection.

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 developers often need to execute various queries. PHP, as a common server‑side language, offers many functions to connect to and manipulate MySQL databases, among which mysqli_query is a frequently used function for executing queries.

The mysqli_query function can run different types of MySQL statements, including SELECT, INSERT, UPDATE, and DELETE. It accepts two parameters: the database connection object and the SQL query string. The following example demonstrates using mysqli_query to perform a SELECT query.

<?php
// 创建数据库连接
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

// 检查连接是否成功
if (! $connection) {
    die('数据库连接失败: ' . mysqli_connect_error());
}

// 执行SELECT查询
$query = "SELECT id, name, age FROM users";
$result = mysqli_query($connection, $query);

// 检查查询是否成功
if (! $result) {
    die('查询失败: ' . mysqli_error($connection));
}

// 处理查询结果
while ($row = mysqli_fetch_assoc($result)) {
    echo 'ID: ' . $row['id'] . ', 姓名: ' . $row['name'] . ', 年龄: ' . $row['age'] . '<br>';
}

// 释放结果集
mysqli_free_result($result);

// 关闭数据库连接
mysqli_close($connection);
?>

The code first creates a database connection and checks its success. It then uses mysqli_query to execute a SELECT statement, storing the result in the $result variable. A 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 data, UPDATE to modify existing data, and DELETE to remove data, simply by passing the appropriate SQL statement as the second argument.

It is important to note that mysqli_query returns a result set object for SELECT queries. Developers 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 operations is convenient and efficient; whether executing SELECT, INSERT, UPDATE, or DELETE statements, providing the correct SQL allows easy interaction with the database.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databasemysqlPHPmysqli_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

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.