nala3 Posted November 5, 2011 Share Posted November 5, 2011 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: Connected Succesfully to the Server Could not select the databse. Trying to create it... Database has been created... Can not select database! 2nd load that should not be needed to select the database: Connected Succesfully to the Server Selected Database Succesfully Quote Link to comment https://forums.phpfreaks.com/topic/250483-will-not-select-database-until-a-reload/ Share on other sites More sharing options...
requinix Posted November 5, 2011 Share Posted November 5, 2011 You're trying to mysql_select_db() the database before you've even created it. Does that sound right? Quote Link to comment https://forums.phpfreaks.com/topic/250483-will-not-select-database-until-a-reload/#findComment-1285146 Share on other sites More sharing options...
Psycho Posted November 5, 2011 Share Posted November 5, 2011 Near the top of the script you attempt to connect to the database before you even check if it exists $select_db = mysql_select_db($db); Then later - after you have created the database if it did not exist - you reuse the variable $select_db to detemine if you show the connection error - instead of reattempting to connect now that the database exists. After you create the databse you need to then attempt to connect to it again. Quote Link to comment https://forums.phpfreaks.com/topic/250483-will-not-select-database-until-a-reload/#findComment-1285148 Share on other sites More sharing options...
nala3 Posted November 5, 2011 Author Share Posted November 5, 2011 You're trying to mysql_select_db() the database before you've even created it. Does that sound right? -snip- Just saw the guy below you had replied with some actually helpful information. Fixed, thank you mjdamato Quote Link to comment https://forums.phpfreaks.com/topic/250483-will-not-select-database-until-a-reload/#findComment-1285150 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.