这是可能的mysqli_multi_query() http://php.net/mysqli_multi_query.
Example:
<?php
$mysqli = new mysqli($host, $user, $password, $database);
// create string of queries separated by ;
$query = "INSERT INTO images (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName');";
$query .= "INSERT INTO images_history (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name, day, month, year) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName', '$day', '$month', '$year');";
// execute query - $result is false if the first query failed
$result = mysqli_multi_query($mysqli, $query);
if ($result) {
do {
// grab the result of the next query
if (($result = mysqli_store_result($mysqli)) === false && mysqli_error($mysqli) != '') {
echo "Query failed: " . mysqli_error($mysqli);
}
} while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); // while there are more results
} else {
echo "First query failed..." . mysqli_error($mysqli);
}
关键是你must use mysqli_multi_query
如果您想在一次调用中执行多个查询。出于安全原因,mysqli_query
不会执行多个查询以防止 SQL 注入。
还要记住以下行为mysqli_store_result
。它返回FALSE
如果查询没有结果集(其中INSERT
查询没有)所以你还必须检查mysqli_error
看到它返回一个空字符串,这意味着INSERT
那是成功的。
See:
mysqli_multi_query http://php.net/mysqli_multi_query
mysqli_more_结果 http://www.php.net/manual/en/mysqli.more-results.php
mysqli_next_结果 http://www.php.net/mysqli_next_result
mysqli_store_结果 http://php.net/mysqli_store_result