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>" ;
}
?>
[PHP5] aDB PDO LIKE Database Abstraction. Switch easily from one db server to another, strong errors management, manage transactions, queries preparation and more. Categories : PHP , PHP Classes , Databases , MS SQL Server , MySQL Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP. Categories : PHP Classes , Databases , PHP , MySQL , InnoDB Link Manager for Link Exchangers Categories : PHP , PHP Classes , Databases , MySQL , CURL Specify your connection settings and create a link to a MySQL database. Categories : PHP , PHP Classes , Databases , MySQL , Beginner Guides A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP , PHP Classes , Databases , MySQL , HTML and PHP Simple database class Categories : PHP , PHP Classes , MySQL , Databases bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL , PHP , MySQL , Complete Programs , Databases Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible Categories : PHP , PHP Classes , Databases , MySQL , MS SQL Server 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 MySQL Handler Categories : PHP , Databases , MySQL , Classes and Objects , PHP Classes This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases , MySQL , Complete Programs , PHP , Databases Simple Mini Poll class library (SimPoll) Categories : PHP , PHP Classes , Databases , MySQL , Complete Programs Simple usersOnline class - keep track of how many users are online on your site Categories : PHP , PHP Classes , Databases , MySQL PHP Transfer data from text file to Mysql Table Categories : PHP , PHP Classes , Filesystem , Databases , MySQL