创建数据库表的文件

问题描述:

I am creating a template that uses a CMS and I will be giving it to other people. The web template requires a couple databases that I created in phpMyAdmin. If someone else is going to be using this they need the same tables, so I was wondering if there is a file I can write that they can install or run on their server that will create the databases and tables? Kind of like when you install Wordpress and it creates their tables for you.

我正在创建一个使用CMS的模板,我将把它交给其他人。 Web模板需要我在phpMyAdmin中创建的几个数据库。 如果其他人打算使用它,他们需要相同的表,所以我想知道是否有一个我可以编写的文件,他们可以在他们的服务器上安装或运行来创建数据库和表? 有点像你安装Wordpress并为你创建他们的表。 p> div>

You can simply export your database from within phpMyAdmin, and then have a PHP file that takes some inputs, like hostname, username, etc, which then runs the SQL dump.

Something like:

<?php

    if(!isset($_POST['hostname']))
        die('<input name="hostname" placeholder="Host name">' .
            '<input name="username" placeholder="Username">' .
            '<input name="password" placeholder="Password">' .
            '<input name="database" placeholder="Database">' .
            '<input type="submit">');

    $dh = mysqli_connect($_POST['hostname'], $_POST['username'],
                         $_POST['password'], $_POST['database']);

    mysqli_multi_query($dh, "-- Your dump file here

                      CREATE TABLE IF NOT EXISTS `mytable` (
                       `mycolumn` VARCHAR(11) NOT NULL
                      ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                      CREATE TABLE IF NOT EXISTS `another` (
                       `hello` INT(4) NOT NULL
                      ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    ");

?>

What about using a mysqldump of tables involved in your cms template? You can check documentation here In this way you will have a dump of your tables' schema