MySQL / SQL_Dump_to_Database


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:

<?php 

// open the .sql file 
$fp = @fopen("mysql.sql""r"); 
$file fread($fp80000); 
fclose($fp); 

$lines explode(';',  $file); // split the .sql into sepearte queries 
$cnt count($lines); // count array elements 

// database connection string 
mysql_connect("localhost","root"); 

// loop each query and execute 
for($j=0; ($j<$cnt-1); $j++) { 
    if(!
mysql_query($lines[$j])) { 
            
// echo any errors 
            
echo "Error on line $j of Query:<br>"
            echo 
$lines[$j]."<br><Br>"
            die; 
    } 

// show confirmation 
echo "Done"
?> 

Description

  If you wish to execute many queries stored in a .sql file (usually created with a MySQL Dump) - this simple script will open the file and execute each query.