T4VN is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support
    HOME  |  HOSTING COUPON  |  TEMPLATE  |  PHP SCRIPTS  |  LINK TO US  |  LINK  |  REGISTER | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Tutotal :::
  PHP Basic (7)
  PHP Advanced (4)
  PHP Database (2)
  Coding Step By Step (8)
  PHP and AJAX (0)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (12)
  Date & Time (5)
  E-Mail (6)
  Forms (5)
  Guestbooks (1)
  Logging (2)
  Miscellaneous (10)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (10)
  User Authentication (6)
::: Search On T4VN :::
::: T4VN Statistics :::
  PHP Scripts : 61
  PHP Example : 67
  PHP Tutorials : 21
  PHP News : 93
  Total Coupon : 36
  Other Tutorials :
  Member : 209
  Visitor Online : 8
  Today Visit: 399
  Total Visitor : 262409
  Most Online : 31
::: Sponsored Links :::

PR 4 For This Webpage
Home Today :

SQL script import function

Author : Ravi Valmikam

Sometimes you might need a function that imports an entire .sql file into the MySQL database. This function will help you do that.

I have two functions:

1. mysql_import_file : simply imports a file as is
2. mysql_install_db : creates a given DB as well as imports a given SQL file for that DB

PHP Example :

<?
/* Accepts a filename and imports the SQL script in it.

   Returns: true if all is well
            false if something is wrong
            (error message is embedded in $errmsg)

   One can also use mysql_error() if this function
   returns an error.

*/

function mysql_import_file($filename, &$errmsg)
{
   
/* Read the file */
   
$lines file($filename);

   if(!
$lines)
   {
      
$errmsg "cannot open file $filename";
      return 
false;
   }

   
$scriptfile false;

   
/* Get rid of the comments and form one jumbo line */
   
foreach($lines as $line)
   {
      
$line trim($line);

      if(!
ereg('^--'$line))
      {
         
$scriptfile.=" ".$line;
      }
   }

   if(!
$scriptfile)
   {
      
$errmsg "no text found in $filename";
      return 
false;
   }

   
/* Split the jumbo line into smaller lines */

   
$queries explode(';'$scriptfile);

   
/* Run each line as a query */

   
foreach($queries as $query)
   {
      
$query trim($query);
      if(
$query == "") { continue; }
      if(!
mysql_query($query.';'))
      {
         
$errmsg "query ".$query." failed";
         return 
false;
      }
   }

   
/* All is well */
   
return true;
}

/* Installs a DB with a given name with the help of a given
   .sql file

   Returns: true if all is well
       false if something is wrong
            (error message is embedded in $errmsg)

   One can also use mysql_error() if this function
   returns an error.

*/

function mysql_install_db($dbname$dbsqlfile, &$errmsg)
{
   
$result true;

   if(!
mysql_select_db($dbname))
   {
     
$result mysql_query("CREATE DATABASE $dbname");
     if(!
$result)
     {
        
$errmsg "could not create [$dbname] db in mysql";
        return 
false;
     }
     
$result mysql_select_db($dbname);
   }

   if(!
$result)
   {
      
$errmsg "could not select [$dbname] database in mysql";
      return 
false;
   }

   
$result mysql_import_file($dbsqlfile$errmsg);

   return 
$result;
}
?>



Usage Example:

PHP Example :

<? 

$link 
mysql_connect "127.0.0.1""root"""); 

if(
mysql_install_db("testdb""testdb.sql"$errmsg)) 

   echo 
"Success!!"
}else 

  echo 
"failure: ".$errmsg."<br/>".mysql_error(); 

?> 


Other Example
Data Type Converter between PHP and PostgreSQL
mssql escape string
Insert/Update/Delete functions
My Batch Query
Count Rows From 2 Tables
mysql syntax error reporting


::: Resources :::
  Links Directory
  Programming 2 3
  Webmaster 2 3
  Web Design 2 3
  Web Hosting 2 3
  Other Links 2 3
  Asian ShowBiz News
  Teach Seo For You
::: New Templates :::




::: Other Tutorials :::
 Program Design

  Powered By T4VN.NET - Version 2.0 - CopyRight © T4VN.NET 2005-2007