[MySqli扩展]⑧--针对多条SAL语句

$mysqli->multi_query($sql)

errno) {
    die("Connect Error " . $mysqli->error);
}
$mysqli->set_charset('UTF8');
$sql = "INSERT user(username,password,age)VALUES('imooc3','imooc3',32);";
$sql .= "UPDATE user set age=5 WHERE id=12;";
$sql .= "DELETE FROM user WHERE id=11;";

//针对多条SQL语句的查询
$res = $mysqli->multi_query($sql);
var_dump($res);

?>
Paste_Image.png

返回值以第一条语句为准 第一条执行成功为TRUE 否则为FALSE
执行失败后 后面的语句不会执行

$mysqli->store_result()

$mysqli->more_results()

$mysqli->next_result()

errno) {
    die('Connnect Error ' . $mysqli->error);
}
$mysqli->set_charset('UTF8');
$sql = "SELECT username FROM user;";
$sql .= "SELECT Host FROM mysql.user;";
$sql .= "SELECT CURRENT_USER();";
$sql .= "SELECT NOW();";
//user_result() / store_result()  获取第一条查询产生的结果集
//more_results() 检测是否有更多的结果集
//next_result() 将结果集指针指向下移动一位
if ($mysqli->multi_query($sql)) {
    do {
        if ($mysqli_result = $mysqli->store_result()) {
            $rows[] = $mysqli_result->fetch_all(MYSQLI_ASSOC);
        }
    } while ($mysqli->more_results() && $mysqli->next_result());
} else {
    echo $mysqli->error;
}
print_r($rows);
$mysqli->close();

?>

三维数组


Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [username] => king
                )

            [1] => Array
                (
                    [username] => king
                )

            [2] => Array
                (
                    [username] => king2
                )

            [3] => Array
                (
                    [username] => queen
                )

            [4] => Array
                (
                    [username] => tong
                )

            [5] => Array
                (
                    [username] => aaaaaa
                )

            [6] => Array
                (
                    [username] => kingaa
                )

            [7] => Array
                (
                    [username] => imooc3
                )

            [8] => Array
                (
                    [username] => imooc3
                )

            [9] => Array
                (
                    [username] => imooc3
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [Host] => 127.0.0.1
                )

            [1] => Array
                (
                    [Host] => ::1
                )

            [2] => Array
                (
                    [Host] => localhost
                )

            [3] => Array
                (
                    [Host] => localhost
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [CURRENT_USER()] => root@localhost
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [NOW()] => 2017-08-30 16:58:26
                )

        )

)

你可能感兴趣的:([MySqli扩展]⑧--针对多条SAL语句)