MySQL Batch File Importing Script



WARNING!

The purpose of this script is to quickly and easily run the commands contained within the kusaba SQL file, and is to be used if you are installing the script for the first time or want to recreate the tables, for whatever reason.
Running this script will delete any related tables and their data (including the admin files). I offer this script as-is and cannot be held responsible for any damages caused or accidental loss of data incurred as a result of running this script.
Before running this script, make sure that:

By clicking this check box I agree that the author of this script cannot be held responsible for my own stupidity if something goes wrong.


".KU_DBPREFIX.$tablename." already exists in the database! Drop it, and re run this script."); } } // Lets open the file for reading! :) echo '

SQL Batch File Processing

'; echo 'Locating \'kusaba_freshinstall.sql\'... '; if (file_exists('kusaba_freshinstall.sql') && (filesize('kusaba_freshinstall.sql') > 0)) { echo 'found.
'; $sqlfile = fopen('kusaba_freshinstall.sql', 'r'); echo 'File opened.
'; $readdata = fread($sqlfile, filesize('kusaba_freshinstall.sql')); $readdata = str_replace('PREFIX_',KU_DBPREFIX,$readdata); fclose($sqlfile); echo 'Contents read.
'; }else{ echo 'error. '; die('An error occured. kusaba_freshinstall.sql does not exist in this directory or it is 0 bytes big :( Barring that, do you have read permissions for the directory?'); } $db->Execute("ALTER DATABASE `" . KU_DBDATABASE . "` CHARACTER SET utf8 COLLATE utf8_general_ci"); // Explodes the array $sqlarray = explode("\n", $readdata); // Loops through the array and deletes the non-SQL bits in the file, which is basically the '--' lines and the lines with no content foreach ($sqlarray as $key => $sqldata) { if (strstr($sqldata, '--') || strlen($sqldata) == 0){ unset($sqlarray[$key]); } } // Here we are imploding everything together again... $readdata = implode('',$sqlarray); // ...then exploding it again. At this point we will have an array where each key's value is a one of the CREATE statements $sqlarray = explode(';',$readdata); echo 'File contents have been formatted for use with mysql_query.
'; // Lets drop any existing tables in the database $listoftables = $db->GetAll("show tables from ".KU_DBDATABASE.""); echo '

Table Creation

'; // Lets now loop through the array and create each table foreach ($sqlarray as $sqldata) { if (strlen($sqldata) !== 0) { // As the array was exploded on ';', the last ';' caused a blank element to be created as there was no data after it :p // The following three lines retrieve the table name of the table from the sql command. It's dynamic so it doesn't matter how many tables need to be created // As long as each CREATE TABLE statement stays in the format CREATE TABLE `table` then this part will work. $pos1 = strpos($sqldata, '`'); $pos2 = strpos($sqldata, '`', $pos1 + 1); $tablename = substr($sqldata, $pos1+1, ($pos2-$pos1)-1); echo "Attempting to create table '$tablename'... "; if($db->Execute($sqldata)) { echo "success.
"; } else { echo "failed. Enable debugging by setting KU_DEBUG to true to see this error.
"; die ("Table creation failed. Please rerun this script again or attempt to fix the problem if you know how to solve it."); } } } // All done :) echo '
SQL commands have finished. If all is well, proceed to the installation file but don\'t forget to delete this file!'; } function mysql_table_exists($database, $tableName) { global $db; $tables = array(); $tablesResults = $db->GetAll("SHOW TABLES FROM `$database`;"); foreach ($tablesResults AS $row) $tables[] = $row[0]; return(in_array($tableName, $tables)); } ?>