Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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 无法使multi_query()工作_Php_Mysqli_Mysqli Multi Query - Fatal编程技术网

Php 无法使multi_query()工作

Php 无法使multi_query()工作,php,mysqli,mysqli-multi-query,Php,Mysqli,Mysqli Multi Query,编辑:工作代码如下 //* opens a connection to a MySQL server */ $mysqli = new mysqli($host, $username, $password, $database); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $que

编辑:工作代码如下

//* opens a connection to a MySQL server */
$mysqli = new mysqli($host, $username, $password, $database);

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}


$query = "INSERT INTO `test` (test1, test2) VALUES('testing','testing2222');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing5555','testing66666');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing77777','testing888888');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing99999','testing101010');";


if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                //printf("%s\n", $row[0]);
            }
            $result->free();
        }
     /* print divider */
    //if ($mysqli->more_results()) {
    //  printf("-----------------\n");
    //}
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();           
下面是错误代码:

我已经尝试了所有可能的方法,但我似乎无法让它发挥作用。我注意到只有当我有一个$query要输入时,一个变体才会起作用。如果它有不止一个,它就会失败

//* opens a connection to a MySQL server */
$mysqli = mysql_connect($host,$username,$password, $database);



$query  = "INSERT INTO `test` (test1, test2) VALUES('testing','testing2222')";
$query  .= "INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444')";


if($mysqli->multi_query($query))
 { 
    do
    {

        if($result=$mysqli->store_result())
        {
            while($row=$result->fetch_row())
            {
                printf("%s<br/>",$row[0]);
            }
            $result->free();
        }

        if($mysqli->more_results())
        {
            print("-------------------------------<br/>");
        }
        else
        {
            echo '<br/>';
        }
    }while($mysqli->more_results() && $mysqli->next_result());
 }
//*打开到MySQL服务器的连接*/
$mysqli=mysql\u connect($host、$username、$password、$database);
$query=“插入'test'(test1,test2)值('testing','testing2222')”;
$query.=“插入'test'(test1,test2)值('testing3333','testing44444')”;
if($mysqli->multi_query($query))
{ 
做
{
如果($result=$mysqli->store_result())
{
而($row=$result->fetch_row())
{
printf(“%s
,$row[0]); } $result->free(); } 如果($mysqli->more_results()) { 打印(“------------------------------------
”); } 其他的 { 回声“
”; } }而($mysqli->more_results()&&$mysqli->next_result()); }

它建立了一个连接,我只是修剪了一些代码,使之更容易理解。表存在,等等。。。也尝试了主要的例子,逐字逐句,但似乎不起作用。我哪里做错了

你必须把
。还要始终检查错误
$mysqli->error

现在,您的查询将与此完全相同

INSERT INTO `test` (test1, test2) VALUES('testing','testing2222')INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444')

这是不正确的。

在每个查询的末尾都需要一个分号。MySQL正在等待语句的结束。请参阅MySQL文档中的以下内容:

一个命令不需要全部在一行中给出,因此需要几行的冗长命令不是问题。mysql通过查找终止分号而不是输入行的结尾来确定语句的结尾。(换句话说,mysql接受自由格式输入:它收集输入行,但在看到分号之前不会执行它们。)

更改此项:

$query  = "INSERT INTO `test` (test1, test2) VALUES('testing','testing2222')";
$query  .= "INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444')";
为此:

$query  = "INSERT INTO `test` (test1, test2) VALUES('testing','testing2222');";
$query  .= "INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444');";

什么语句缺少@布里安夫。您的第一次插入。@BrianV.-不是你的PHP代码缺少分号,而是SQL语句。@BrianV。你不应该因为问题/代码已经解决而修改它。其他可能访问此问题的人将不知道问题原来在哪里,除非他们看到编辑。因此,将我的答案视为不完整,包括另一个。@Fred ii-仍然存在错误的代码和说明,那么问题出在哪里?请尝试在“INSERT”语句中使用不存在的表来查看脚本的反应。我的意思是,你不应该在没有错误处理的情况下使用脚本。