安装脚本 - 如何检查数据库表是否已存在?

时间:2010-07-31 14:40:25

标签: mysql database

我发现如何使用此代码查找数据库是否存在:

if(mysql_select_db('db-name'))
return true;

但我想查看数据库中的表格。这是代码:

error_reporting(0);
session_start();

global $_ERROR;

$_ERROR = array();
$installed = false;

if($_POST)
{
    $num = 0;
    foreach($_POST as $key=>$value)
    {
        if(strlen(str_replace(" ","",$value))<=0 && $key!='db_pass')
        {
            $_ERROR[$key] = 'class="error"';
            $num ++;
        }
    }
    if($num>0)
    {
        $_ERROR['text'] = 'Please fill in all fields';
    }
    else
    {
        // Create Database
        $con = mysql_connect($_POST['db_server'],$_POST['db_user'],$_POST['db_pass']);
        if (!$con) {
            $_ERROR['text'] = 'There was an error connecting to your database';
        }
        else
        {
            if(mysql_select_db($_POST['db_name'])){

            }
            $db = mysql_select_db($_POST['db_name'],$con);
            if(!$db)
            {
                $_ERROR['text'] = 'Could not select database';
            }
            else
            {

                foreach($_POST as $key=>$value)
                {
                    $_POST[$key] = mysql_real_escape_string($value,$con);
                }

                $sql = 'SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
                        CREATE TABLE `active_guests` (
                        `ip` varchar(15) collate latin1_general_ci NOT NULL,
                        `timestamp` int(11) unsigned NOT NULL,
                        PRIMARY KEY  (`ip`)
                        );';
                mysql_query($sql,$con) or die(mysql_error());
                $sql = 'CREATE TABLE `active_users` (
                        `username` varchar(30) collate latin1_general_ci NOT NULL,
                        `timestamp` int(11) unsigned NOT NULL,
                         PRIMARY KEY  (`username`)
                        );';
                mysql_query($sql,$con) or die(mysql_error());
                $sql = 'CREATE TABLE `banned_users` (
                        `username` varchar(30) collate latin1_general_ci NOT NULL,
                        `timestamp` int(11) unsigned NOT NULL,
                        PRIMARY KEY  (`username`)
                        );';
                mysql_query($sql,$con) or die(mysql_error());
                $sql = "CREATE TABLE `forum` (
                        `postid` bigint(20) NOT NULL auto_increment,
                        `author` varchar(255) collate latin1_general_ci NOT NULL default '',
                        `title` varchar(255) collate latin1_general_ci NOT NULL default '',
                        `post` mediumtext collate latin1_general_ci NOT NULL,
                        `showtime` varchar(255) collate latin1_general_ci NOT NULL default '',
                        `realtime` bigint(20) NOT NULL default '0',
                        `lastposter` varchar(255) collate latin1_general_ci NOT NULL default '',
                        `numreplies` bigint(20) NOT NULL default '0',
                        `parentid` bigint(20) NOT NULL default '0',
                        `lastrepliedto` bigint(20) NOT NULL default '0',
                        `author_avatar` varchar(30) collate latin1_general_ci NOT NULL default 'default',
                        `type` varchar(2) collate latin1_general_ci NOT NULL default '1',
                        `stick` varchar(6) collate latin1_general_ci NOT NULL default '0',
                        `numtopics` bigint(20) NOT NULL default '0',
                        `cat` bigint(20) NOT NULL,
                        PRIMARY KEY  (`postid`)
                        );";
                mysql_query($sql,$con) or die(mysql_error());
                $sql = "CREATE TABLE `messages` (
                        `id` int(11) NOT NULL auto_increment,
                        `reciever` varchar(25) NOT NULL default '',
                        `sender` varchar(25) NOT NULL default '',
                        `subject` text NOT NULL,
                        `message` longtext NOT NULL,
                        `recieved` enum('1','0') default '0',
                        PRIMARY KEY  (`id`)
                        );";
                mysql_query($sql,$con) or die(mysql_error());           
                $sql = "CREATE TABLE `news` (
                        `id` int(11) NOT NULL auto_increment,
                        `titre` varchar(255) collate latin1_general_ci NOT NULL,
                        `contenu` text collate latin1_general_ci NOT NULL,
                        `timestamp` bigint(20) NOT NULL default '0',
                        PRIMARY KEY  (`id`)
                        );";
                mysql_query($sql,$con) or die(mysql_error());   
                $sql = "CREATE TABLE `settings` (
                        `id` int(11) NOT NULL auto_increment,
                        `name` varchar(25) NOT NULL default '',
                        `value` varchar(20) NOT NULL,
                        PRIMARY KEY  (`id`)
                        );";
                mysql_query($sql,$con) or die(mysql_error());
                $sql = "CREATE TABLE `users` (
                        `username` varchar(30) collate latin1_general_ci NOT NULL,
                        `password` varchar(32) collate latin1_general_ci default NULL,
                        `userid` varchar(32) collate latin1_general_ci default NULL,
                        `userlevel` tinyint(1) unsigned NOT NULL,
                        `email` varchar(50) collate latin1_general_ci default NULL,
                        `timestamp` int(11) unsigned NOT NULL,
                        `web` varchar(90) collate latin1_general_ci default 'Sorry, i dont have a website.',
                        `country` varchar(90) collate latin1_general_ci NOT NULL,
                        `avatar` varchar(30) collate latin1_general_ci NOT NULL default 'default',
                        `status` varchar(50) collate latin1_general_ci NOT NULL,
                        `pm_count` int(11) NOT NULL default '0',
                        `statusmessage` mediumtext COLLATE latin1_general_ci NOT NULL,
                        PRIMARY KEY (`username`)
                        );";        
                mysql_query($sql,$con) or die(mysql_error());
                $pass = md5($_POST['password']);
                $sql = "INSERT INTO `users` VALUES ('$_POST[name]', '$pass', '0', '9', '$_POST[email]', '', '', '', 'default', '', '0', 'Hello world!')";
                mysql_query($sql,$con) or die(mysql_error());
                $sql = "INSERT INTO `settings` VALUES (1, 'update', '0')";
                mysql_query($sql,$con) or die(mysql_error());
                $sql = "INSERT INTO `settings` VALUES (2, 'title', '$_POST[sitename]')";
                mysql_query($sql,$con) or die(mysql_error());
                $sql = "INSERT INTO `settings` VALUES (3, 'frontnews', '0')";
                mysql_query($sql,$con) or die(mysql_error());
                $sql = "INSERT INTO `settings` VALUES (4, 'headercolor', '#7387a7')";
                mysql_query($sql,$con) or die(mysql_error());
                $sql = "INSERT INTO `settings` VALUES (5, 'numpms', '50')";
                mysql_query($sql,$con) or die(mysql_error());


                $file = fopen("db.php","r");
                $data = fread($file, 500000);
                fclose($file);

                $data = str_replace('define("INSTALLED",FALSE);','define("INSTALLED",TRUE);',$data);
                $data = str_replace('HOST',$_POST['db_server'],$data);
                $data = str_replace('USER_',$_POST['db_user'],$data);
                $data = str_replace('PASSWORD',$_POST['db_pass'],$data);
                $data = str_replace('DBNAME',$_POST['db_name'],$data);
                $data = str_replace('EMAIL_NAME',$_POST['email_name'],$data);
                $data = str_replace('_EMAIL',$_POST['email'],$data);

                $file = fopen("db.php","w");
                fwrite($file, $data, 500000);
                fclose($file);

                header("Location: ../index.php");
            }
        }
    }
}

$_POST['siteurl'] = str_replace("/includes/install.php","","http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

?>

其余的是html,所以没用。基本上,如果我在安装后再次运行此安装脚本,它将给出一个错误,表示表“活动来宾”已经存在。 (它创建了第一个表,所以它总是显示出来。)

我正在尝试添加对表的检查以告诉脚本:表已经存在,所以不要修改它们,只需创建db.php文件而不触及数据库。

2 个答案:

答案 0 :(得分:1)

您可以使用show tables SQL查询来检查现有表:

$result = mysql_query("show tables like '$tablename'"); 

答案 1 :(得分:1)

你可以修改你的SQL:

CREATE TABLE IF NOT EXISTS `tablename`

Reference MySQL docs here