Install script - How to check if database tables already exist?
I found out how to find if the database exists with this code:
if(mysql_select_db('db-name'))
return true;
But I would like to check the tables inside the database. Here is the code:
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` 开发者_如何学JAVAbigint(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']);
?>
The rest is html, so it's no use. Basically, if i run this install script again after installing, it will give me an error saying that the table "active guest" already exists. (its the first table created, so its always this one showing.)
I'm trying to add a checking of the tables to tell the script: the tables already exist so don't modify them and just create the db.php file without touching the database.
You can just amend your SQL:
CREATE TABLE IF NOT EXISTS `tablename`
Reference MySQL docs here.
You can use show tables
SQL query to check for existing table:
$result = mysql_query("show tables like '$tablename'");
精彩评论