Hongmu Notes
Home Language Notes PHP executes multiple sql statements at once
Language Notes PHP PHP and mysql

PHP executes multiple sql statements at once

PHP executes multiple sql statements at once

In PHP, this can be done by mysqli_multi_query() The function executes multiple SQL statements in a single call.

Here is an example code snippet:

<?php
// 定义 MySQL 数据库连接参数
$host = "localhost";
$user = "root";
$password = "password";
$dbname = "mydatabase";

// 创建 MySQL 数据库连接
$conn = mysqli_connect($host, $user, $password, $dbname);

// 检查连接是否成功
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}

// 要执行的 SQL 语句,多条语句以分号隔开
$sql = "INSERT INTO mytable (id, name, age) VALUES (1, 'Alice', 18);"
     . "INSERT INTO mytable (id, name, age) VALUES (2, 'Bob', 20);"
     . "INSERT INTO mytable (id, name, age) VALUES (3, 'Charlie', 22);";

// 执行多条 SQL 语句
if (mysqli_multi_query($conn, $sql)) {
    echo "执行成功";
} else {
    echo "执行失败: " . mysqli_error($conn);
}

// 关闭 MySQL 数据库连接
mysqli_close($conn);
?>

In this example code, a MySQL database connection is first established, followed by the definition of the SQL statements to be executed—these statements are separated by semicolons. Finally, the code is used. mysqli_multi_query() The function executes multiple SQL statements in a single call.

Please note that,mysqli_multi_query() When a function executes multiple SQL statements, if any single statement fails to execute, all subsequent statements will also be terminated. Therefore, it is essential to carefully examine and validate the SQL statements before invoking this function to ensure their correctness and reliability.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 181
mysqli in PHP

mysqli in PHP Language Notes PHP PHP collection PHP and mysql

The `mysqli_num_rows()` function is exclusively used with `SELECT` query methods, whereas the `mysqli_affected_rows()` function returns the number of rows affected by the previous SQL statement across the entire database; this function is primarily used with `INSERT`, `UPDATE`, and `DELETE` operations.
👁 146

Recommended reading

(Adaptive Mobile Version) Cooling Tower Website Template | Refrigeration Equipment Website Source Code – 1096

(Adaptive Mobile Version) Cooling Tower Website Template | Refrigeration Equipment Website Source Code – 1096 Practical Collection pbootcms Template

A PbootCMS website template for cooling towers and refrigeration equipment, compatible with both PC and WAP devices. The professional industrial design is ideal for showcasing cooling tower products, refrigeration systems, and project cases. It enables refrigeration equipment manufacturers to present their products online and attract both industrial and commercial clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 43
(PC + WAP) Black plastic sheet website template – pbootcms website template; PC plastic sheet website source code download – 0131

(PC + WAP) Black plastic sheet website template – pbootcms website template; PC plastic sheet website source code download – 0131 Practical Collection pbootcms Template

This PbootCMS template is designed for the plastic sheet and PC sheet industries and supports access via both PC and WAP devices. Its sophisticated and professional design effectively showcases the characteristics of plastic sheet products and their engineering applications. It enables plastic sheet manufacturers to showcase their product specifications and capabilities online, helping them attract customers from the industrial and construction sectors. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 38
Responsive solid wood furniture website template 0931

Responsive solid wood furniture website template 0931 Practical Collection Yiyou template

An eyouCMS responsive website template designed specifically for the solid wood furniture industry. Its premium, natural design style effectively showcases solid wood furniture products, brand stories, design concepts, and customisation services. This template helps solid wood furniture brands showcase their products online and attract consumers who value a quality lifestyle. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions Regarding eyouCMS Installation...
👁 64
(Adaptive Mobile Version) Website Optimization – SEO Blogging Website Template – With Ad Slots – 1097

(Adaptive Mobile Version) Website Optimization – SEO Blogging Website Template – With Ad Slots – 1097 Practical Collection pbootcms Template

A SEO-optimized PbootCMS website template for blogs, compatible with both PC and WAP devices, featuring built-in advertising slots. The clean and informative design makes it ideal for SEO bloggers to share optimization techniques, industry trends, and case studies. The integrated advertising slots enable bloggers to monetize their blog traffic. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 67
(PC+WAP) black doors and windows custom website pbootcms template hardware and building materials industry website source code download 0132

(PC+WAP) black doors and windows custom website pbootcms template hardware and building materials industry website source code download 0132 Practical Collection pbootcms Template

A PbootCMS website template designed for the custom window and door manufacturing as well as the hardware and building materials industry, compatible with both PC and mobile devices. Its modern and sophisticated design effectively showcases your window and door product ranges as well as your strong presence in the building materials trade. This template helps window and door brands or building materials companies create a professional online brand presence, thereby enhancing their marketing and promotional effectiveness. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 46
Wood-grain flooring & wall tile website template – 0933

Wood-grain flooring & wall tile website template – 0933 Practical Collection Yiyou template

An eYouCMS website template designed for the wood-grain flooring and wall tile industries. Its modern home decoration design style is ideal for showcasing flooring products, wall tile collections, spatial arrangements, and brand stories. This template helps building materials brands showcase their products online and attract both renovation companies and homeowners. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eYouCMS | eYouCMS...
👁 62