Mastering PHP’s mysqli_query: Quick Guide to SELECT, INSERT, UPDATE, DELETE

This article explains how to use PHP's mysqli_query function to connect to MySQL, execute SELECT queries with result handling, and also perform INSERT, UPDATE, and DELETE operations, providing clear code examples and best‑practice tips for backend developers.

php Courses
php Courses
php Courses
Mastering PHP’s mysqli_query: Quick Guide to SELECT, INSERT, UPDATE, DELETE

MySQL is a widely used relational database management system, and PHP provides functions to connect and operate on MySQL databases. The mysqli_query function is a common way to execute queries such as SELECT, INSERT, UPDATE, and DELETE.

The following example demonstrates how to create a MySQL connection with mysqli_connect, run a SELECT query using mysqli_query, check for errors, fetch rows with mysqli_fetch_assoc, and output each record’s ID, name, and age.

<?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);
?>

The code first creates and verifies the database connection, then stores the SELECT result in $result. It iterates over $result to print each row’s fields, finally freeing the result set and closing the connection.

Beyond SELECT, mysqli_query can execute INSERT, UPDATE, DELETE, and other SQL statements by passing the appropriate query string as the second argument.

Note that mysqli_query returns a result set object for SELECT queries; you can retrieve rows using mysqli_fetch_assoc, mysqli_fetch_row, or mysqli_fetch_array depending on the desired format.

In summary, using PHP’s mysqli_query provides a convenient and fast way to perform MySQL operations, whether selecting, inserting, updating, or deleting data.

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.