SunQuest
 
           PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old February 25th, 2004, 10:34 AM
Jake_Gibilaro Jake_Gibilaro is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 5 Jake_Gibilaro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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,

Reply With Quote
  #2  
Old February 25th, 2004, 01:01 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
You've got a couple of options. One is to change mysql_query($RED); to

PHP Code:
 mysql_query($RED) or die(mysql_error); 


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:
 $bad_queries=array();

//All your file operations here. The following line goes in your foreach loop.

mysql_query($RED) or array_push($bad_queries,$RED); 


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.

Reply With Quote
  #3  
Old February 26th, 2004, 09:18 AM
Jake_Gibilaro Jake_Gibilaro is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 5 Jake_Gibilaro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks alot. Putting bad queries in an array really

helps to see what is going on!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Validating Queries when restoring database(PHP)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway