Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 15-04-2009
Member
 
Join Date: Apr 2009
Posts: 61
Import .sql file via PHP

Please,

I have several files with .SQL extension for simplicity its all database related files and i wanted to fetch those file from the MySQL database using PHP.

I know it is possible through the command prompt but i don't want to go for that option as it will again increase the headache, so please provide if anyone has the proper solution.

Thanks
Reply With Quote
  #2  
Old 15-04-2009
Member
 
Join Date: Apr 2008
Posts: 193
Re: Import .sql file via PHP

You can try something like this:

Code:
// let's pretend that connection to server is established
// and database chosen...
$sql = explode(';#%%', file_get_contents ('backup.sql'));
$n = count ($sql) - 1;
for ($i = 0; $i < $n, $i++) {
$query = $sql[$i];
$result = mysql_query ($query)
or die ('<p>Query: <br><tt>' . $query .
'</tt><br>failed. MySQL error: ' . mysql_error());
}
Note that I am using ';#%%' to break the file into queries simply because your backup script seems to insert it after each query.Normally, queries would end with alone.

Also note that the for() cycle is written so that the last member of the $sql array is not executed, since that member does not contain any SQL (there can be no SQL after the last occurrence of).
Reply With Quote
  #3  
Old 15-04-2009
Member
 
Join Date: Feb 2009
Posts: 78
Re: Import .sql file via PHP

You may also use the following code :

Assuming each instruction is on a line.. (untested)
Code:
$db = mysql_connect(....);
mysql_select_db(....);

$fp = fopen('somefile.sql', 'r');
while($fp != feof())
{
$line = fread($fp, 2048);
$line = mysql_real_escape_string($db, $line);
mysql_query($line);
}
fclose($fp);
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Import .sql file via PHP"
Thread Thread Starter Forum Replies Last Post
Need help to import a Microsoft Excel file to semicolon delimited text file Roch Windows Software 5 13-01-2012 05:18 PM
How to import registry string value from *.csv file or ini file Aalap Operating Systems 7 02-06-2011 10:06 PM
Processing file and import Linux-Us Windows Software 3 11-12-2009 11:36 AM
wmm won't import MPG file gemss Windows XP Support 2 06-12-2009 09:12 PM
Can't import avi video file! Need help! sayeed Windows XP Support 3 27-05-2008 11:11 PM


All times are GMT +5.5. The time now is 10:36 PM.