mysqli查询上的多个命令不起作用?

时间:2018-09-23 12:34:12

标签: php mysql mysqli

如果我写这样的查询:

$conn = new mysqli($db_host, $db_username, $db_password, $db_database);
$conn -> query ("
        DROP TABLE IF EXISTS for_search;
        CREATE TABLE IF NOT EXISTS for_search
        (
          id INT,
          for_search TEXT
        );
");
$conn -> close();

它不起作用,但是如果我这样重写:

$conn = new mysqli($db_host, $db_username, $db_password, $db_database);
$conn -> query ("
        DROP TABLE IF EXISTS for_search;
");    
$conn -> query ("
        CREATE TABLE IF NOT EXISTS for_search
        (
          id INT,
          for_search TEXT
        );
");
$conn -> close();

它将起作用! 我想知道为什么我不能在mysqli中编写多行查询吗?

1 个答案:

答案 0 :(得分:1)

在mysqli中,您应该使用multi_query来运行几个查询:

$conn = new mysqli($db_host, $db_username, $db_password, $db_database);
    $conn -> multi_query ("
        DROP TABLE IF EXISTS for_search;
        CREATE TABLE IF NOT EXISTS for_search
        (
          id INT AUTOINCREMENT,
          for_search TEXT
        );
    ");
    $conn -> close();