Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-22-2010, 11:39 AM   PM User | #1
starvos
New Coder

 
Join Date: Aug 2009
Posts: 31
Thanks: 4
Thanked 4 Times in 4 Posts
starvos is an unknown quantity at this point
Problem with creating new database/tables using php

I have saved the following codes as ini.php and tables.sql basically consist of mysql tables creation/insertion commands. However, I have to run the php script twice, first run will only give me the new database but no tables, only after the second run will I get the tables with the data inserted.
PHP Code:
    $user_name "user";
    
$pass_word "pass";
    
$database "db";
    
$server "localhost";
    
$db_handle mysql_connect($server$user_name$pass_word) or die ("unable to connect to server");
    
$db_found mysql_select_db($database$db_handle) or die (mysql_query("CREATE DATABASE $database"));
    
$queryFile 'tables.sql';
    
$fp    fopen($queryFile'r');
        
$sqlFile fread($fp,filesize($queryFile));
        
$sqlArray explode(';',$sqlFile);
    foreach (
$sqlArray as $query) {
        if (
strlen($query)>3){
            
$result mysql_query($query,$db_handle);
                if (!
$result){
                
$sqlErrorCode mysql_errno();
                
$sqlErrorText mysql_error();
                
$sqlquery      $query;
                break;
            }
        }
    }     
    
fclose($fp); 
Can any expert out there please tell me what have I done wrongly? And how can I achieve the creation of database & tables in one single run? Thanks in advance~
starvos is offline   Reply With Quote
Old 03-22-2010, 02:21 PM   PM User | #2
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
right here;

$db_found = mysql_select_db($database, $db_handle) or die (mysql_query("CREATE DATABASE $database"));


your trying to select the data base, and if it doesn't exist you create it, however then you try to continue on without selecting the new database,

see what I mean? to fix, use the select db again after you create it.
angst is offline   Reply With Quote
Users who have thanked angst for this post:
starvos (03-23-2010)
Old 03-23-2010, 02:17 AM   PM User | #3
starvos
New Coder

 
Join Date: Aug 2009
Posts: 31
Thanks: 4
Thanked 4 Times in 4 Posts
starvos is an unknown quantity at this point
Where should I place the select db command? I tried after this line but it gives me the same problem.
PHP Code:
$db_found mysql_select_db($database$db_handle) or die (mysql_query("CREATE DATABASE $database")); 
Maybe I shouldn't try creating the database after "or die"?
starvos is offline   Reply With Quote
Old 03-23-2010, 02:26 AM   PM User | #4
starvos
New Coder

 
Join Date: Aug 2009
Posts: 31
Thanks: 4
Thanked 4 Times in 4 Posts
starvos is an unknown quantity at this point
Smile

Nevermind, I have solved the problem by changing the line
PHP Code:
$db_found mysql_select_db($database$db_handle) or die (mysql_query("CREATE DATABASE $database")); 
to

PHP Code:
    $db_found mysql_select_db($database$db_handle);// or die (mysql_query("CREATE DATABASE $database"));
    
if (!$db_found){
        
mysql_query("CREATE DATABASE $database");
    }
    else {
        
$db mysql_select_db($database$db_handle);
    } 
Thanks for pointing out to use the select db again~
starvos is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:11 AM.


Advertisement
Log in to turn off these ads.