For a fixed query with no user-supplied values, query() is fine. For variable values, use a prepared statement. During development, exceptions make failures easier to diagnose; production pages should log technical details rather than print database errors to visitors.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = new mysqli('localhost', 'db_user', 'db_password', 'database_name');
$connection->set_charset('utf8mb4');
try {
$connection->query("UPDATE student SET class = 'Five' WHERE class = 'Four'");
echo 'Records updated: ' . $connection->affected_rows;
} catch (mysqli_sql_exception $e) {
error_log($e->getMessage());
echo 'The database operation could not be completed.';
}
?>
$connection->query() method executes a fixed SQL statement. For values from forms or URLs, use a prepared statement instead.
$query = "CREATE TABLE student (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
class VARCHAR(10) NOT NULL,
mark INT NOT NULL DEFAULT 0,
PRIMARY KEY (id)
) ENGINE=InnoDB";
The SQL CREATE TABLE statement is stored in $query and passed to a
parameter to the function like below.
$rt = $connection->query($query); // For this fixed CREATE TABLE statement.
For a successful CREATE TABLE command, query() returns true. When strict MySQLi error reporting is enabled (as in the complete example above), SQL errors throw mysqli_sql_exception instead of being returned as false. Use try/catch to handle failures. If you need a refresher on branching, see PHP if/else.try {
$connection->query($query);
echo 'Command completed successfully.';
} catch (mysqli_sql_exception $e) {
error_log($e->getMessage());
echo 'Unable to complete the database operation.';
}
Log technical details server-side inside the catch block. Do not display raw SQL errors, usernames, credentials or internal table names to website visitors. The short line below belongs inside catch (mysqli_sql_exception $e).
error_log($e->getMessage()); // Inside catch (mysqli_sql_exception $e).
During development, inspect the server error log. In production, show the visitor a generic message while keeping database details private. See MySQLi query examples for the related API. The SQL error reference provides additional historical background; do not copy removed functions from legacy examples.
$query = "UPDATE student SET class = 'Five' WHERE class = 'Four'";
try {
$connection->query($query); // Fixed SQL only; prepare queries with user input.
echo 'Records updated: ' . $connection->affected_rows;
} catch (mysqli_sql_exception $e) {
error_log($e->getMessage());
echo 'Unable to update records.';
}
These shorter fragments assume the $connection object is created as shown in the complete example above; see the MySQLi connection lesson.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| vexatiousjones | 04-02-2016 |
| Im getting a No Database Selected error when using this code. Im using PDO connection string linked aboe. Any Suggestions? | |
| smo1234 | 06-02-2016 |
| YOu need to connect to database and select database. Read the connection string link available here. | |
| Ann Ekpe | 26-03-2017 |
| Good day . please I need your assistance.I am currently designing a webpage with members signing in and having a dashboard.I want to be able to send data from the database to each particular member , so anytime they login to the website they can view the data... Urgent HELP | |
| smo1234 | 26-03-2017 |
| You can store the message in a table and display inside login area, that is when member login is completed this message can be displayed. The message can be managed by adding or updating new message etc. Knowledge of any scripting language and Database handling is required. | |