两个mysqli查询

是否有可能有两个mysqli_queries像这样?

mysqli_query($dblink, "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')") or die(mysql_error()); mysqli_query($dblink, "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')") or die(mysql_error()); 

基本上我想更新我的数据库中的两个表。 有没有更好的方法来做到这一点?

这是可能的与mysqli_multi_query() 。

例:

 <?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); } 

关键是如果你想在一次调用中执行多个查询,你必须使用mysqli_multi_query 。 出于安全原因, mysqli_query不会执行多个查询来防止SQL注入。

还要记住mysqli_store_result的行为。 如果查询没有结果集(哪些INSERT查询不这样做),则返回FALSE ,因此您还必须检查mysqli_error ,看看是否返回了一个空string,表示INSERT成功。

看到:
mysqli_multi_query
mysqli_more_results
mysqli_next_result
mysqli_store_result

一劳永逸! 使用此函数可以在脚本中的任何位置获取无限数量的查询结果。

function:

您只需将多重查询的输出传递给函数,并返回在每个查询中find的所有结果和错误。

  function loop_multi($result){ //use the global variable $conn in this function global $conn; //an array to store results and return at the end $returned = array("result"=>array(),"error"=>array()); //if first query doesn't return errors if ($result){ //store results of first query in the $returned array $returned["result"][0] = mysqli_store_result($conn); //set a variable to loop and assign following results to the $returned array properly $count = 0; // start doing and keep trying until the while condition below is not met do { //increase the loop count by one $count++; //go to the next result mysqli_next_result($conn); //get mysqli stored result for this query $result = mysqli_store_result($conn); //if this query in the loop doesn't return errors if($result){ //store results of this query in the $returned array $returned["result"][$count] = $result; //if this query in the loop returns errors }else{ //store errors of this query in the $returned array $returned["error"][$count] = mysqli_error($conn); } } // stop if this is false while (mysqli_more_results($conn)); }else{ //if first query returns errors $returned["error"][0] = mysqli_error($conn); } //return the $returned array return $returned; } 

用法:

 $query = "INSERT INTO table1 (attribute1) VALUES ('value1');"; $query .= "INSERT INTO table2 (attribute2) VALUES ('value2');"; $query .= "SELECT * FROM table3;"; //execute query $result = mysqli_multi_query($conn, $query); //pass $result to the loop_multi function $output = loop_multi($result); 

产量

$ output包含2个数组“result”和“error”,按查询sorting。 例如,如果您需要检查执行第三个查询时是否发生错误并获取结果,您可以执行以下操作:

 if(isset($output['error'][2]) && $output['error'][2] !== ""){ echo $output['error'][2]; }else{ while($row = $output['result'][2]->fetch_assoc()) { print_r($row); } }