gpt4 book ai didi

php - 如何使用单个查询通过 PHP 创建多个 MySQL 表?

转载 作者:行者123 更新时间:2023-11-29 07:12:52 25 4
gpt4 key购买 nike

我正在尝试为我的网站创建一个“设置脚本”。我想创建数据库,同时添加表格和一些内容。到目前为止,这就是我的做法,但使用多个查询似乎有点困惑:

<?php


$servername = "localhost";
$username = "root";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Create database
$sql = "CREATE DATABASE MYDB";
if ($conn->query($sql) === TRUE) {

echo "1. Database created successfully <br/>";
$conn->select_db("MYDB");


$sql_members = "CREATE TABLE MEMBERS (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
USERNAME VARCHAR(30) NOT NULL,
EMAIL VARCHAR(40) NOT NULL,
DISCOUNT VARCHAR(5),
PASSW CHAR(128),
ROLE VARCHAR(9)
)";


if ($conn->query($sql_members) === TRUE) {
echo "2. Table MEMBERS created successfully <br/>";
} else {
echo "Error creating table: " . $conn->error;
}

$sql_content = "CREATE TABLE CONTENT (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
TITLE VARCHAR(30) NOT NULL,
TEXT VARCHAR(30) NOT NULL
)";

if ($conn->query($sql_content) === TRUE) {
echo "3. Table CONTENT created successfully <br/>";
} else {
echo "Error creating table: " . $conn->error;
}



} else {
echo "Error creating database: " . $conn->error;
}

$conn->close();


?>

还有更好的办法吗?谢谢!

==更新==

我尝试导出数据库并使用生成的 .sql 文件作为我的设置查询,但出现了问题,我得到:

Error creating tables: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO CONTACTS (ID, NAME, PHONE, EMAIL, ADDRESS, CITY, `COUN' at line 12

CREATE TABLE IF NOT EXISTS `CONTACTS` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(25) COLLATE utf8_romanian_ci NOT NULL,
`PHONE` varchar(16) COLLATE utf8_romanian_ci NOT NULL,
`EMAIL` varchar(35) COLLATE utf8_romanian_ci NOT NULL,
`ADDRESS` text COLLATE utf8_romanian_ci NOT NULL,
`CITY` varchar(16) COLLATE utf8_romanian_ci NOT NULL,
`COUNTRY` varchar(16) COLLATE utf8_romanian_ci NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_romanian_ci AUTO_INCREMENT=2 ;

INSERT INTO `CONTACTS` (`ID`, `NAME`, `PHONE`, `EMAIL`, `ADDRESS`, `CITY`, `COUNTRY`) VALUES
(1, 'Peter Brown', '0742062307', 'office@shop.com', 'Avenue 13.', 'Santaclaus', 'Austria');

==解决方案==

我需要“multi_query()”来执行多个查询。

最佳答案

你也可以尝试一下:p

$errors = [];

$table1 = "CREATE TABLE MEMBERS (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
USERNAME VARCHAR(30) NOT NULL,
EMAIL VARCHAR(40) NOT NULL,
DISCOUNT VARCHAR(5),
PASSW CHAR(128),
ROLE VARCHAR(9)
)";

$table2 = "CREATE TABLE CONTENT (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
TITLE VARCHAR(30) NOT NULL,
TEXT VARCHAR(30) NOT NULL
)";


$tables = [$table1, $table2];


foreach($tables as $k => $sql){
$query = @$conn->query($sql);

if(!$query)
$errors[] = "Table $k : Creation failed ($conn->error)";
else
$errors[] = "Table $k : Creation done";
}


foreach($errors as $msg) {
echo "$msg <br>";
}

关于php - 如何使用单个查询通过 PHP 创建多个 MySQL 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38997569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com