带有CREATE TABLE和INSERT INTO的mysqli_multi_query

时间:2014-04-01 11:54:00

标签: php mysqli create-table insert-into mysqli-multi-query

$query = "INSERT INTO abc VALUES ('1', '2', '3')";
$send_to_mysql = mysqli_query($connection, $query);

行。

$query = "CREATE TABLE abc (a varchar(255), b varchar(255), c varchar(255))";
$send_to_mysql = mysqli_query($connection, $query);

行。

$query = "CREATE TABLE abc (a varchar(255), b varchar(255), c varchar(255))";
$query .= "INSERT INTO abc VALUES ('1', '2', '3')";
$send_to_mysql = mysqli_multi_query($connection, $query);

NOP。问题出在哪里?

2 个答案:

答案 0 :(得分:2)

在两个查询之间加一个分号。当您将两个查询放在一个单独的字符串中时,需要将它们分开,以便MySQL了解您确实有两个查询:

$query = "CREATE TABLE abc (a varchar(255), b varchar(255), c varchar(255));";
$query .= "INSERT INTO abc VALUES ('1', '2', '3')";

答案 1 :(得分:0)

将分号作为两个查询之间的分隔符,例如

$query = "CREATE TABLE abc (a varchar(255), b varchar(255), c varchar(255));";
mysqli_multi_query($connection, $query);
$query = "INSERT INTO abc VALUES ('1', '2', '3')";
$send_to_mysql = mysqli_multi_query($connection, $query);