Home >Database >Mysql Tutorial >Can Multiple SQL UPDATE Queries Be Executed with a Single `mysqli_multi_query()` Call?
Executing Multiple SQL Statements with mysql_query()
In the realm of MySQL database operations, it may be desirable to execute multiple SQL statements in a single mysql_query() call. The question arises: is it possible to combine update queries within the same mysql_query() function?
Approach
Historically, the mysql_query() function did not support the execution of multiple statements in a single call. However, since the release of PHP 5.5, the mysql_ functions have been deprecated and replaced with mysqli_ functions.
The mysqli::multi_query() function offers the ability to execute multiple SQL statements consecutively. It is important to note that this function should be used with caution, as it potentially increases the vulnerability to SQL injection attacks.
Usage
To execute multiple update statements within mysqli::multi_query(), you can use the following syntax:
mysqli_multi_query($mysqli, "UPDATE table SET name = 'bob'; UPDATE table SET age = 55 WHERE name = 'jim';");
This will execute both update statements in sequence, allowing you to modify multiple rows and columns without the need for separate mysql_query() calls.
The above is the detailed content of Can Multiple SQL UPDATE Queries Be Executed with a Single `mysqli_multi_query()` Call?. For more information, please follow other related articles on the PHP Chinese website!