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

时间:2016-08-17 13:08:08

标签: php mysql subquery setup-deployment create-table

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

<?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();


?>

有更好的方法吗? 谢谢!

== UPDATE ==

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

  

创建表时出错:SQL语法中有错误;检查   手册,对应右边的MySQL服务器版本   要在&{39} INSERT INTO CONTACTSIDNAMEPHONE附近使用的语法,   EMAILADDRESSCITY,`COUN&#39;在第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');

== SOLUTUION ==

我需要&#34; multi_query()&#34;用于执行我的多个查询。

3 个答案:

答案 0 :(得分:2)

您也可以尝试: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>";
}

答案 1 :(得分:0)

您可以export the whole database including all tables使用命令行或使用PhPMyAdmin。然后在php中查询文件的内容以创建数据库。

答案 2 :(得分:0)

你可以创建一个文件并将所有的sql查询放入其中..

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)
);

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

然后在你的PHP代码中:

$query = file_get_contents ('queries.sql');

if ($conn->query($query) === TRUE) {
    echo "all tables created successfully <br/>";
} else {
    echo "Error creating tables: " . $conn->error;
}