|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Validating Queries when restoring database(PHP)
I'm using the following code (PHP) to restore
my database from a .sql file (posted by mike_r - http://forums.devarticles.com/archive/t-6007) $FP = fopen ( $file, 'r' ); $READ = fread ( $FP, filesize ( $file) ); $READ = explode ( ";\n", $READ ); foreach ( $READ AS $RED ) { mysql_query ( $RED ); } What I would like to do is somehow validate this: i.e. give an error if there was something wrong with the .sql file but seeing as INSERT queries don't return anything i can't see how to do this. Would appreciate any help Cheers, |
|
#2
|
||||
|
||||
|
You've got a couple of options. One is to change mysql_query($RED); to
PHP Code:
The problem with that is that it'll insert all the rows before the first one to fail, so that the next time you run the script, you'll either get primary key errors or duplicate results. Another option is to do something like the following: PHP Code:
That'll run all the good queries and give you a list of the bad ones to print out (probably to another file) and fix. Once you think you've got them fixed, just run the script again, this time directing it to the new file with the fixed queries. The final option, if you've got innodb tables, is to use transactions, which will give you an all or nothing result: If any query fails, they all roll back. I'd probably opt for the second solution.
__________________
Please don't PM me asking for solutions outside the scope of a thread. Keeping all responses in a thread stands to help others who come along later, which is after all what this forum's all about. |
|
#3
|
|||
|
|||
|
Thanks alot. Putting bad queries in an array really
helps to see what is going on! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Validating Queries when restoring database(PHP) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|