Re: Import .sql file into MySQL DB ?

This is a discussion on Re: Import .sql file into MySQL DB ? within the PHP Language forums, part of the PHP Programming Forums category; Jerry T <Jerry@ nothanks.com> wrote in message news:<drgkjv03hrpc69p2tt5v0pjmfdspqlnesf@4ax.com>. .. > > Using a ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-13-2003
Nikolai Chuvakhin
 
Posts: n/a
Default Re: Import .sql file into MySQL DB ?

Jerry T <Jerry@ nothanks.com> wrote in message
news:<drgkjv03hrpc69p2tt5v0pjmfdspqlnesf@4ax.com>. ..
>
> Using a script I found, I can get data out of my MySQL DB,
> but how do I get it back in ...


phpMyAdmin has a functionality to execute *.sql files.

> the .sql file is like this :
>
> DROP TABLE IF EXISTS columns_priv;#%%
> CREATE TABLE columns_priv (
> Host char(60) binary NOT NULL,
> Db char(64) binary NOT NULL,
> User char(16) binary NOT NULL,
> Table_name char(64) binary NOT NULL,
> Column_name char(64) binary NOT NULL,
> Timestamp timestamp(14),
> Column_priv set('Select','Insert','Update','References') NOT NULL,
> PRIMARY KEY (Host, Db, User, Table_name, Column_name)
> );#%%

[the rest of the file skipped]

You can try somthing like this:

// 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 backupo 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 ';').

Cheers,
NC
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:12 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0