简体   繁体   English

如何使用mysqli-> multi_query()?

[英]How to use mysqli->multi_query()?

Can anyone show me how to use mysqli->multi_query() to execute multiple update/ insert queries on a single connection? 谁能告诉我如何使用mysqli-> multi_query()在单个连接上执行多个更新/插入查询? Thanks. 谢谢。

I did follow the tutorials in the PHP manual. 我确实遵循了PHP手册中的教程。 But I have some issues. 但是我有一些问题。

My batch of queries has 5 statements separated by semicolons. 我的查询批处理中有5条用分号分隔的语句。

UPDATE scenes set UserID = '11111111' WHERE ID = '031DFDAD92F6F4AB64AF317C06D64089DF119EC2';

INSERT INTO surfaces (ID, Name, SceneID, SurfaceTypeID, Color) VALUES('5F9A7301C2D398C4D1B90BA5AA56A9DED3FAA639', 'front ', '031DFDAD92F6F4AB64AF317C06D64089DF119EC2', 1, 11432044);

INSERT INTO regions (ID, SurfaceID, x, y, width, height, RegionMovieClip) VALUES('864406A2CB30CFBE846ED7B0B08A79BD5605037D', '5F9A7301C2D398C4D1B90BA5AA56A9DED3FAA639', 375, 22, 104, 125, 'asdasdcvxcv');

INSERT INTO surfaces (ID, Name, SceneID, SurfaceTypeID, Color) VALUES('1FCA2131ED1B89206E4E66DBE20D8D09513FF39D', 'floor ', '031DFDAD92F6F4AB64AF317C06D64089DF119EC2', 1, 7318465);

INSERT INTO regions (ID, SurfaceID, x, y, width, height, RegionMovieClip) VALUES('DBD0E85EAEE2685E2AEC590C8CA214C3C5653971', '1FCA2131ED1B89206E4E66DBE20D8D09513FF39D', 272, 288, 114, 89, 'asdasd')

All are executed except for the 3rd query. 除第三个查询外,全部执行。 That's where I'm lost. 那就是我迷路的地方。 If there's an sql error inserting the 3rd query, how can I get it? 如果插入第3个查询时出现SQL错误,如何获取? And how the queries after the failed 3rd one is getting executed? 失败的第三个查询之后的查询如何执行?

The examples don't really show how to handle an error. 这些示例并未真正显示如何处理错误。

Execution will stop after the first failure. 第一次失败后,执行将停止。

Use mysqli_error() to get error information. 使用mysqli_error()获取错误信息。

mysqli_use_result() returns false if an error occured - if you count the results and there aren't enough, an error occurred. 如果发生错误, mysqli_use_result()将返回mysqli_use_result()如果计算结果的数量不足,则会发生错误。

This is not fool proof, but I have fought and fought with MySQLi and it's band of merry men involved with multi_query and I couldn't get it to play nicely the way I wanted to, or have the flexibility that I needed. 这并不是万无一失的方法,但是我已经和MySQLi进行过战斗,并且这是一群乐于参与multi_query的人,我无法使其以我想要的方式很好地发挥,或者无法获得所需的灵活性。 I saw several examples where some programmers were simply running explode(';', $sql_statements) which made my eyes bleed with how horribly wrong that can be. 我看到了一些示例,其中一些程序员只是在运行explode(';', $sql_statements) ,这使我explode(';', $sql_statements)

My solution may not work for you, but this worked for me. 我的解决方案可能对您不起作用,但这对我有用。 (no it's not bulletproof either, but does the job for my particular application). (不,它也不是防弹的,但是可以完成我的特定应用程序的工作)。

<?php
    $file = file_get_contents('test_multiple_queries.sql');
    $result = preg_split("/;(?=\s*(create|insert|update|alter|show|explain|truncate|drop|delete|replace|start|lock|commit|rollback|set|begin|declare|rename|load|begin|describe|help))/im", $file);
    $result = array_map('trim', $result);

    foreach($result as $sql_query) {

        // Procedural style
        mysqli_query($link, $sql_query);

        // Now you can get errors easily, or affected_rows, or whatever
        //    using much simpler, readable code
        mysqli_error($link);
        mysqli_affected_rows($link);

        // or go crazy with some other stuff
        $words = preg_split("/\s+/", $sql_query);
        switch(strtolower($words[0])) {
            case 'insert':
                // do something nifty like...
                echo 'New ID: '.mysqli_insert_id($link)."\n";
                break;
            case 'drop':
                // obviously run this before the query, simply here for example
                echo 'Hey young (man|lady)! We don\'t drop anything!';
                break;
        }
    }

This is untested and will not handle errors but should run all 5 queries. 这未经测试,不会处理错误,但是应该运行所有5个查询。

$fconn = new mysqli($fdbhost, $fdbuser, $fdbpass, $fdbname) or die  ('Error connecting to mysqli');
$query  = "UPDATE scenes set UserID = '11111111' WHERE ID = '031DFDAD92F6F4AB64AF317C06D64089DF119EC2';";
$query .= "INSERT INTO surfaces (ID, Name, SceneID, SurfaceTypeID, Color) VALUES('5F9A7301C2D398C4D1B90BA5AA56A9DED3FAA639', 'front ', '031DFDAD92F6F4AB64AF317C06D64089DF119EC2', 1, 11432044);";
$query .= "INSERT INTO surfaces (ID, Name, SceneID, SurfaceTypeID, Color) VALUES('1FCA2131ED1B89206E4E66DBE20D8D09513FF39D', 'floor ', '031DFDAD92F6F4AB64AF317C06D64089DF119EC2', 1, 7318465);";
$query .= "INSERT INTO surfaces (ID, Name, SceneID, SurfaceTypeID, Color) VALUES('1FCA2131ED1B89206E4E66DBE20D8D09513FF39D', 'floor ', '031DFDAD92F6F4AB64AF317C06D64089DF119EC2', 1, 7318465);";
$query .= "INSERT INTO regions (ID, SurfaceID, x, y, width, height, RegionMovieClip) VALUES('DBD0E85EAEE2685E2AEC590C8CA214C3C5653971', '1FCA2131ED1B89206E4E66DBE20D8D09513FF39D', 272, 288, 114, 89, 'asdasd')";
if ($fconn->multi_query($query)) {

         if ($result = $fconn->store_result()) {
            //while ($row = $result->fetch_row()) {
                //print_r($row);
            }
            //$result->free();
            }
         if ($fconn->more_results()) {

            while ($fconn->next_result()){ 
            $thisresult = $fconn->store_result();

            print_r($thisresult).'<br />';
            }
            }

}
unset($query);
$fconn->close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM