SQL script works in MySQL, but fails with mysqli->

2019-03-02 05:53发布

问题:

My SQL script works successfully in MySQL Workbench.

The following gives an error:

$link = mysqli_connect(***********);
$result = $link->query($sql);

The SQL script performs some actions including:

  • create temporary table
  • truncate table
  • drop table
  • insert
  • select

The error is:

-- 1.1 Creating temporary table for categories

    drop table if exists exp_categories; -- just in case
    create temporary table exp_categories
    (
        ID bigint not null,
        Categories text not null,
        PRIMARY KEY (ID)
    ) DEFAULT CHARSET=utf8;

-- 1.2 Inserting data...
-- few other statements here
-- last statement is SELECT

Does mysqli have limitations that I am encountering?

回答1:

Try to use multi_query()

Multiple statements or multi queries must be executed with mysqli_multi_query(). The individual statements of the statement string are separated by semicolon.

$link->multi_query($sql)

And the user you connect with have to have necessary privileges in your db or global privileges.



标签: mysqli