Results 1 to 3 of 3

Thread: Import .sql file via PHP

  1. #1
    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

  2. #2
    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).

  3. #3
    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);

Similar Threads

  1. Replies: 5
    Last Post: 13-01-2012, 05:18 PM
  2. Replies: 7
    Last Post: 02-06-2011, 10:06 PM
  3. Processing file and import
    By Linux-Us in forum Windows Software
    Replies: 3
    Last Post: 11-12-2009, 11:36 AM
  4. wmm won't import MPG file
    By gemss in forum Windows XP Support
    Replies: 2
    Last Post: 06-12-2009, 09:12 PM
  5. Can't import avi video file! Need help!
    By sayeed in forum Windows XP Support
    Replies: 3
    Last Post: 27-05-2008, 11:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,933,512.34755 seconds with 17 queries