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

Learn how to use PHP’s mysqli_query function to connect to MySQL, execute SELECT queries, handle results, and perform INSERT, UPDATE, and DELETE operations, with a complete code example and best‑practice tips for managing database connections and error handling.

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 several functions to connect to and operate on MySQL databases. The mysqli_query function is one of the most common ways to execute queries.

It can run various types of SQL statements, including SELECT , INSERT , UPDATE and DELETE . The function takes two arguments: the database connection object and the SQL query string. Below is a sample code that demonstrates 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'] . ', Name: ' . $row['name'] . ', Age: ' . $row['age'] . '<br>';
}

// Free result set
mysqli_free_result($result);

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

The script first creates a database connection and verifies it succeeded. It then runs a SELECT statement, stores the result in the $result variable, and checks for errors. Using a while loop with mysqli_fetch_assoc, it iterates over each row, printing the ID, name, and age. Finally, it frees the result set and closes the connection.

Beyond SELECT , mysqli_query can execute other statements such as INSERT to add records, UPDATE to modify existing data, and DELETE to remove records—simply pass the appropriate SQL as the second argument.

When mysqli_query runs a query, it returns a result set object for retrieval. You can fetch rows in different formats using functions like mysqli_fetch_assoc, mysqli_fetch_row, or mysqli_fetch_array.

In summary, using PHP’s mysqli_query to perform MySQL operations is straightforward and efficient. Whether you need to SELECT, INSERT, UPDATE, or DELETE data, providing the correct SQL statement lets you manipulate the database with minimal code.

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.

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