|
|
|
|
|
|
| |
This is a simple class that can parse text files with a list of MySQL query statements to be executed.
The class splits the file lines, skips comment lines and gathers all consecutive lines until the one ends with semi-colon to get the complete SQL statement.
Each statement is executed as MySQL query to the current default MySQL database connection.
| <?php
/**
* @author : MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
* Moderator, phpResource (http://groups.yahoo.com/group/phpresource/)
* URL: http://www.rupom.info
* @version : 1.0* Version : 1.0
* Date : 20 May, 2006
* Purpose : Importing valid SQL dump to DB
* Release : Released under GNU Public License
*/
class ParseSql
{
var $file;
function ParseSql($file)
{
$this->setFile($file);
$this->startParsing();
}
/**
* @purpose : Sets filename to be parsed
* @params $file
* @return none
*/
function setFile($file)
{
$this->file = $file;
}
/**
* @purpose : Gets filename to be parsed
* @params none
* @return filename
*/
function getFile()
{
return $this->file;
}
/**
* @purpose : Parses SQL file
* @params none
* @return none
*/
function startParsing()
{
$file = $this->getFile();
// Getting the SQL file content
$content = file_get_contents($file);
// Processing the SQL file content
$file_content = explode("\n",$content);
$query = "";
// Parsing the SQL file content
foreach($file_content as $sql_line)
{
if(trim($sql_line) != "" && strpos($sql_line, "--") === false)
{
$query .= $sql_line;
// Checking whether the line is a valid statement
if(preg_match("/(.*);/", $sql_line))
{
$query = substr($query, 0, strlen($query)-1);
//Executing the parsed string, returns the error code in failure
$result = mysql_query($query)or die(mysql_error());
$query = "";
}
}
} //End of foreach
return true;
} //End of function
} //End of class
//Usage of the class
mysql_connect("hostname","username","password") or die('Cant Connect...');
mysql_select_db("yourdb") or die('DB Connection Problem...');;
$parseObj = new ParseSql('your_sql_file.sql');
$res = $parseObj->startParsing();
if($res)
{
echo "<b>Query Executed Successfully.</b>";
}
?> | | |
|
| usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | | | PostGreSQL and MySQL 2 in 1 db Manager Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL | | | MySQL Class to ease Database connectivity Categories : MySQL, PHP Classes, Databases, PHP | | | Simple Mini Poll class library (SimPoll) Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs | | | Specify your connection settings and create a link to a MySQL database. Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides | | | Online Automatic Class Generator for MySQL Tables Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL | | | Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | Simple usersOnline class - keep track of how many users are online on your site Categories : PHP, PHP Classes, Databases, MySQL | | | Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP. Categories : PHP Classes, Databases, PHP, MySQL, InnoDB | | | Ajax PHP Tree (Left and Right) with MySQL Categories : PHP, Databases, MySQL, AJAX, PHP Classes | | | YellowPages Content Grabber (PHP5 +) Categories : PHP, PHP Classes, Regexps, Databases, MySQL | | | Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server | | | MySQL Connection/Query Class Categories : Databases, MySQL, PHP, PHP Classes | | | Create and restore backup of MySQL databases Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs | |
|
|
|