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~