Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么可以';我不能运行两个mysqli查询吗?第二个失败了_Php_Mysqli - Fatal编程技术网

Php 为什么可以';我不能运行两个mysqli查询吗?第二个失败了

Php 为什么可以';我不能运行两个mysqli查询吗?第二个失败了,php,mysqli,Php,Mysqli,有可能有两个这样的mysqli查询吗 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'

有可能有两个这样的mysqli查询吗

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')");
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')");
基本上我想更新数据库中的两个表。有更好的方法吗?

这是可能的

例如:

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

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

功能:

$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);
您只需将多重查询的输出传递给函数,它就会返回在每个查询中找到的所有结果和错误

  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个数组“结果”和“错误”。例如,如果在执行第三个查询并获取其结果时需要检查是否发生任何错误,则可以执行以下操作:

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

关于堆栈溢出的一些答案是如此自相矛盾,以至于令人震惊

关键是,如果要在一次调用中执行多个查询,必须使用mysqli\u multi\u query出于安全原因,mysqli_query不会执行多个查询以防止SQL注入

它基本上是这样说的:“关键是你必须在没有安全锁具的情况下使用枪支,因为常规武器不允许你射中自己的一只脚。因此,这里有一种方法可以打破它,现在你可以在一次射中使自己致残!”

尽管OP没有询问如何在一次呼叫中运行两个查询,尽管引用了明确警告,即在一次呼叫中运行多个查询的能力具有固有的危险性,但该回答却提供了绕过此限制的方法

最糟糕的是,所有这些危险和辛苦的混乱都是徒劳的。原因很简单,在一次调用中运行多个查询没有单一的原因。逐个运行查询是使用数据库API的方式

基本上我想更新数据库中的两个表。有更好的方法吗

当然可以。只需使用两个准备好的查询。 它不仅更加干净,而且100%安全,不受SQL注入的影响


如果您的一个查询失败,只需。

可以运行两个查询,当然可以。您说这是可能的,但上面的方法不起作用。它只是插入到图像中,而不是插入到图像历史记录中。也许您应该检查特定的查询是否存在问题,这与任何其他查询都无关。@PartisanEntity您应该将
mysql\u error()
更改为
mysqli\u error()
,因为两者不同且
mysql\u error()当
mysqli
查询失败时,
不会返回任何消息。当然,您可以运行两个或十个或任意数量的查询。数字无关紧要。问题与号码无关。执行第二个查询时只出现了一个错误。我知道这很旧,但对于仍然使用mysqli_multi_query的人来说,正如@dre010所解释的:出于安全原因,mysqli_query不会执行多个查询以防止SQL注入。所以我认为你应该考虑把你的查询分成几个单独的查询,并使用准备好的StimeMeTimes对此感到困惑。我试图对此进行调整,以便我可以说,‘如果通过搜索第一个表找到字符串,则返回结果。’。如果在搜索第一个表时找不到,在搜索第二个表时也找不到,则将其插入第二个表中。因此,请运行第一个SELECT语句,但仅在第一个SELECT语句未返回任何结果时才运行第二个SELECT语句。有没有办法区分您希望在mysqli_multi_查询中使用哪个SELECT语句?@DjDaihatsu我想说的是,不要使代码过于复杂,只需将两个SELECT作为单独的查询编写,而不要使用multi_SELECT。根据您的表结构或选择列表,区分的方法是查看结果中返回了哪些行,您将知道它来自哪个语句,但这会增加额外的不必要逻辑,如果您只需对mysqli_query进行两次单独调用,就可以避免这些逻辑。谢谢@drew010!因此,在关闭连接之前,可以在数据库中的不同表上运行多个单独的select语句吗?我一定是在代码中的某个地方犯了另一个错误,因为我无法从第二个表(在同一数据库中)返回结果。@DjDaihatsu完全可以接受。使用单独的结果集,您甚至可以对其他表发出单独的查询,同时仍然使用其他结果。但是,在同一个连接上对不同的表发出多个选择是完全可以的。一句话:使用
mysqli\u multi\u查询
可以让您自己备份SQL注入攻击。为了安全起见,不要调用
mysqli\u multi\u query
。相反,要求调用方将每个单独的查询作为自己的字符串传递。所以输入参数应该是一个查询字符串数组。然后,分别调用
mysqli\u query
。您仍然可以像以前那样打包结果,但这将避免一些但不是所有的SQL注入攻击。[为避免所有攻击,请参数化所有查询。]