WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : SQL Parsing Class - parse text files with a list of MySQL query statements to be executed.
Categories : PHP, PHP Classes, Databases, MySQL Click here to Update Your Picture
MA Razzaque Rupom
Date : May 20th 2006
Grade : 5 of 5 (graded 3 times)
Viewed : 4474
File : 4403.php
Images : No Images for this code example.
Search : More code by MA Razzaque Rupom
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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