Using PHP mysqli_query to Execute MySQL Queries

This article introduces the PHP mysqli_query function, demonstrates how to connect to a MySQL database, execute SELECT, INSERT, UPDATE, and DELETE statements, and explains result handling with example code, providing a concise guide for backend developers working with relational databases.

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 perform various queries. PHP, a common server‑side language, provides functions to connect to and operate MySQL databases, with mysqli_query being a primary function for executing queries.

The mysqli_query function can execute many types of MySQL statements, including SELECT, INSERT, UPDATE, and DELETE. It accepts two parameters: the database connection object and the SQL query string. Below is an example that performs 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 example code first creates a database connection and checks its success, then runs a SELECT query and stores the result in $result. It iterates over $result to output each row’s ID, name, and age, finally freeing the result set and closing the connection.

Beyond SELECT, mysqli_query can execute other statements such as INSERT, UPDATE, and DELETE by passing 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 to obtain different result formats.

In summary, using PHP’s mysqli_query function provides a convenient and efficient way to perform common database operations, making it a valuable tool for backend developers working with MySQL.

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.

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