Search code examples
phpmysqlsubquerysetup-deploymentcreate-table

How to create multiple MySQL tables via PHP using a single query?


I am trying to create a "setup script" for my website. I would like to create the database, adding tables and some content at the same time. So far this is how I did it, but it seems kind off messy using multiple queries:

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


?>

Is there a better way? Thanks!

== UPDATE ==

I have tried to export the database and use the resulted .sql file as my setup query, but something is wrong, I get:

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', '[email protected]', 'Avenue 13.', 'Santaclaus', 'Austria');

== SOLUTUION ==

I needed "multi_query()" for executing my multiple queries.


Solution

  • You can try this too :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>";
    }