[英]Run multiple CREATE TABLe queries with MySQL and PHP
I need to create a large number of tables at once and add data to the first one using PHP and MySQL. 我需要一次创建大量表,并使用PHP和MySQL将数据添加到第一个表中。 My code looks like this: 我的代码如下所示:
$sql = "CREATE TABLE table1 (
code VARCHAR(5) PRIMARY KEY,
name VARCHAR(50) NOT NULL
);";
//adding businesses to table
$sql .= "INSERT INTO table1 (code, name)
VALUES ('CODE', 'name');";
//^this basic idea is run a few more times
$sql .= "CREATE TABLE table2 (
code VARCHAR(5) PRIMARY KEY,
name VARCHAR(50) NOT NULL
);";
//^same basic thing run a bunch more times w/ variations to columns
if ($conn->multi_query($sql) === TRUE) {
echo "<p>Tables created.</p>
<a href=\"adduser_form.php\">Continue</a>";
} else {
echo "<p>Error creating tables: " . $conn->error . "</p>";
}
This only creates the first table and adds the data to it, it won't create any other tables and I get no error messages (the success message is shown). 这只会创建第一个表并向其中添加数据,不会创建任何其他表,并且我不会收到任何错误消息(显示成功消息)。 I've been googling a solution for an hour and I can't come up with anything. 我已经搜索了一个小时的解决方案,但我什么都没想。 I'm thinking I need a way to hold off creating the next table until the previous one has been created? 我在想我需要一种方法来推迟创建下一个表,直到创建上一个表?
I'm very new to MySQL so newbie-friendly would be very much appreciated:) 我是MySQL的新手,因此非常感谢新手:)
Try using backticks in your SQL statements, such as: 尝试在SQL语句中使用反引号,例如:
$sql .= "CREATE TABLE `table2` (
`code` VARCHAR(5) PRIMARY KEY,
`name` VARCHAR(50) NOT NULL
);";
You should also use backticks in your INSERT statement (and your first CREATE query). 您还应该在INSERT语句(和第一个CREATE查询)中使用反引号。
If I'm not mistaken, name
is reserved word. 如果我没记错的话, name
是保留字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.