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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
