PHP Modules Tutorials - Herong's Tutorial Examples - v5.19, by Herong Yang
mysqli_query() - Run SQL Queries
This section provides a tutorial example on how to run a SQL query on a MySQL server by calling the mysqli_query() function.
Once you have created a MySQL connection, you can run SQL queries using the mysqli_query() function:
$res = mysqli_query($con, $sql); where: $con - The MySQL connection object $sql - The SQL query statement string $res - The query execution result
Ther are several other functions related to mysqli_query():
mysqli_error($con) - Returns the error message from the query mysqli_errno($con) - Returns the error number from the query
Here is a PHP script example showing you how to use the mysqli_query() function:
<?php
# mysqli-query.php
#- Copyright 2009-2026 (c) HerongYang.com. All Rights Reserved.
$con = mysqli_connect("127.0.0.1", "herong", "TopSecret");
# drop an existing database
$res = mysqli_query_err($con, "DROP DATABASE MyBase");
# create a new database
$res = mysqli_query_err($con, "CREATE DATABASE MyBase");
# set the current database
$res = mysqli_query_err($con, "USE MyBase");
# create a new table
$res = mysqli_query_err($con,
"CREATE TABLE MyTable ".
"(ID INTEGER AUTO_INCREMENT, Value INTEGER, PRIMARY KEY (ID))");
# query the table structure
$res = mysqli_query_err($con, "DESC MyTable");
# display the header row
$fields = mysqli_fetch_fields($res);
foreach ($fields as $field) {
print(str_pad($field->name, 10));
}
print("\n");
# display result rows
while ($row = mysqli_fetch_array($res, MYSQLI_NUM)) {
foreach ($row as $val) {
print(str_pad($val, 10));
}
print("\n");
}
mysqli_free_result($res);
mysqli_close($con);
function mysqli_query_err($con, $sql) {
$res = mysqli_query($con, $sql);
if (!$res) {
$errno = mysqli_errno($con);
$error = mysqli_error($con);
print("Query failed: $errno - $error\n");
}
return $res;
}
If you run the script, you will get the following output:
herong$ php mysqli-query.php Query failed: 1008 - Can't drop database 'mybase'; database doesn't exist Field Type Null Key Default Extra ID int(11) NO PRI auto_increment Value int(11) YES
The error message is expected, because "MyBase" does no exist initially.
Table of Contents
Introduction and Installation of PHP
Managing PHP Environment and Modules on macOS
Managing PHP Environment and Modules on CentOS
DOM Module - Parsing HTML Documents
GD Module - Manipulating Images and Pictures
►MySQLi Module - Accessing MySQL Server
MySQLi Module and Configuration
mysqli-hello.php - Test MySQLi Module
mysqli_connect() - Open MySQL Connection
►mysqli_query() - Run SQL Queries
mysqli_affected_rows() - Count Affected Rows
mysqli_insert_id() - ID from AUTO_INCREMENT
mysqli_fetch_array() - Fetch Row from Query Result
mysqli_fetch_fields() - Fetch Field Information
Escape Escape Characters - \\\\ for \\
Summary of Basic MySQLi Functions
MySQLi Object-Oriented Programming
OpenSSL Module - Cryptography and SSL/TLS Toolkit
PCRE Module - Perl Compatible Regular Expressions
SOAP Module - Creating and Calling Web Services
SOAP Module - Server Functions and Examples