Ok, so I am trying to make a database with PHP and I hit a snag. First off, here is the code in question.
<?php
//GET CONFIG
include_once('../scripts/config.inc.php');
//SET VARIABLES TO SOMETHING A BIT SHORTER FOR THE DATABASE OPTIONS
$host = $config['db_settings']['db_host'];
$user = $config['db_settings']['db_user'];
$pass = $config['db_settings']['db_pass'];
$prefix = $config['db_settings']['db_prefix'];
$db = $config['db_settings']['db_name'];
$con = mysql_connect($host, $user, $pass);
$select_db = mysql_select_db($db);
$c_tables = '';
$c_database = "CREATE DATABASE IF NOT EXISTS $db;";
//NOW CONNECT TO THE SERVER
if ($con){
echo('Connected Succesfully to the Server <br />');
}
else die('Could not connect to database: ' . mysql_error());
//NOW CONNECT TO THE DATABASE
if ($select_db){
echo('Selected Database Succesfully <br />');
}
//IF DATABASE COULD NOT BE SELECTED THEN TRY TO MAKE IT
else
if(!$select_db){
echo('Could not select the databse. Trying to create it... <br />');
if(mysql_query($c_database)){
echo('Database has been created...<br />');
if($select_db){
echo('Database Has Been Selected.');
}
else die('Can not select database!' . mysql_error());
}
else die('Could not created the database!');
}
else die ('Could not create or select the database!: ' . mysql_error() . '<br />');
//NOW CREATE THE TABLES
mysql_close;
?>
The problem is, that if I drop the database to test that it creates the database, it does indeed make it, but after it makes the database it will not select it until I reload the page and intern the script. So this is the output I would get. (The fact that there is no error is what is causing me to be confused)
1st load that makes new database:
2nd load that should not be needed to select the database: