Jump to content

Will not select database until a reload?


nala3

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.