Using PHP mysqli_query to Perform MySQL Queries

This article explains how the PHP mysqli_query function works with MySQL, provides a complete example for executing SELECT queries, and discusses its use for INSERT, UPDATE, DELETE operations along with result handling and related fetching functions.

php Courses
php Courses
php Courses
Using PHP mysqli_query to Perform MySQL Queries

MySQL is a widely used relational database management system, and when developing web applications PHP provides functions to connect and operate MySQL, among which the mysqli_query function is commonly used to execute queries.

The function can run SELECT, INSERT, UPDATE, DELETE and other SQL statements; it takes a database connection object and an SQL string as parameters. Below is a sample PHP script that uses mysqli_query to perform 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'] . ', 姓名: ' . $row['name'] . ', 年龄: ' . $row['age'] . '<br>';
}

// Free result set
mysqli_free_result($result);

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

The script first creates a connection, checks for success, defines a SELECT statement, executes it with mysqli_query, checks for errors, iterates over the result set with mysqli_fetch_assoc to print each row's ID, name and age, then frees the result and closes the connection.

In addition to SELECT, the same function can execute INSERT, UPDATE, DELETE and other statements by passing the appropriate SQL string as the second argument.

The function returns a result set object for queries that produce rows; developers can retrieve data using mysqli_fetch_assoc, mysqli_fetch_row, mysqli_fetch_array, and similar functions.

Overall, using PHP's mysqli_query provides a convenient way to interact with MySQL databases for various CRUD operations.

Java learning material download

C language learning material download

Frontend development learning material download

C++ learning material download

PHP learning material download

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.

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