Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 PHP Code: Import SQL Data Jump to:  
Category: >> PHP Code >> Import SQL Data Bookmark and Share

<< lastnext >>

Snippet Name: Import SQL Data

Description: This script will create tables and load data from an sql file.

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: February 27th, 2009

<?PHP
 
FUNCTION run_query_batch($handle, $filename=""){
 
  // Open SQL file.
  IF (! ($fd = FOPEN($filename, "r")) ) {
    DIE("Failed to open $filename: " . MYSQL_ERROR() . "<br>");
  }
 
  // Iterate through each line in the file.
  WHILE (!FEOF($fd)) {
 
    // Read next line from file.
    $line = FGETS($fd, 32768);
    $stmt = "$stmt$line";
 
    // Semicolon indicates end of statement, keep adding to the statement.
    // until one is reached.
    IF (!PREG_MATCH("/;/", $stmt)) {
      CONTINUE;
    }
 
    // Remove semicolon and execute entire statement.
    $stmt = PREG_REPLACE("/;/", "", $stmt);
 
    // Execute the statement.
    MYSQL_QUERY($stmt, $handle) ||
      DIE("Query failed: " . MYSQL_ERROR() . "<br>");
 
    $stmt = "";
  }
 
  // Close SQL file.
  FCLOSE($fd);
}
 
run_query_batch($dbhandle, "schema.sql");
 
?>


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 220 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?