Guys, Please, i need your help. I created a module which is a backup and restore. The strategy i did was im extracting data from mysql and save it to a textfile. The format of text inside the textfile is SQL statement or exactly the same with the EXPORT result of phpmyadmin. Now my problem is with restoring. Im reading the textfile and i used semi-colon as a separator of the SQL statements. Example insert into CountryCodes (ccode, cname, countrySHORT) values (964, "Iraq", "IQ"); insert into CountryCodes (ccode, cname, countrySHORT) values (353, "Ireland (Irish Republic; Eire)", "IE"); the first insert was successful but the second is not because of the semicolon near Irish Republic value. Any idea on what should i do? Thanks again, Mike
<? $read = fopen( 'backup.sql', 'r' ); while( !feof( $read ) ) { $sql = fgets( $read, 4096 ) ; if( !mysql_query( $sql ) ) { die( sprintf( 'mysql_query( "%s" ) failed', $sql ) ); } else { printf("%s\r\n", $sql ); } } fclose( $read ); ?> PHP:
Hi Krackjoe, Thanks for your quick help. Im just wondering, what will be your delimiter let say the textfile contains 10 or more insert statement of lines. i think mysql_query cant query many insert statement in 1 call, example $sql="INSERT INTO table(val) VALUE(1); INSERT INTO table(val) VALUE(2);"; mysql_query($sql); Thanks, Mike
There is no need to delimit anything, it will read one line at a time and execute it, fgets automatically reads upto a newline ( \n ) OR until length bytes have been read - whichever comes first.
Hi Guys, still no luck, i cant restore my generated sql textfile http://gigamike.net/test01.txt it seems my textfile created same as the output of phpmyadmin export. I cant figure it out on how i can restore it without using any delimiters. Please help guys, Mike
that's because the CREATE TABLE statements aren't on one line. why can't you just use phpmyadmin to restore it ?? alternatively, use preg_replace() to remove comments and execute it as one statement, assuming your server allows you to upload files larger than the backup
Hi krakjoe, it works fine now, i removed the\n\r and comments since im the one generating the backup textfile. I want to create my own backup and restore utility inorder for my admin to use it without the coder help. Thanks again, Mike