Jump to content

How to execute all or no inserts/updates in MySQL


poshpaws

Recommended Posts

Hi,

 

I have a script where I update multiple tables. For example: products, product_spec, product_type, order etc. The way I do this is that I just execute the mysql_query("x") in succession. If one fails, it returns an error and halts the script.

 

I've had situations where the first few queries were successful, and then one fails so the rest dont execute. As the tables reference each other, this causes errors when I try to view the data and the only workaround is to manually delete the entries in the DB.

 

So I was wondering, is there a way to 'test' a query first, without actually executing it? I want to make sure that all queries will be successful before executing them, so either all are executed or none. I use mysql_insert_id from previous queries, to store in other tables, so I'm wondering how this would be done.

Link to comment
Share on other sites

By test I mean, to somehow test if a query would work without actually executing it. This is to get around problems like this:

 

1st "INSERT INTO User..." - executes fine, no errors

 

2nd "INSERT INTO User_group.." - error here

 

Sometimes I may have 5/6 insert statements, which makes it trickier. Particularly using mysql_insert_id.

Link to comment
Share on other sites

It happened a while ago, and I think it was to do with one of the ID's not passing correctly.

 

My question was more general, just to see if it was possible to do something like this. Although validation should take care of this, sometimes it falls through the net and I wanted to be sure there would be no probs with the DB.

Link to comment
Share on other sites

Well, if it's programmer error, it's silly for the same programmer to write code to catch these errors!  But you could easily deal with if/else blocks everywhere if you really wanted to.  Also, you can "test" SQL statements with LIMIT 0, and see what the parser thinks about your statement.

Link to comment
Share on other sites

Thanks for the link..this is what I was wanting to do. I've read about this before (ACID etc), but never really applied it.

 

Do any of you use transactions with mysql for updates/inserts? How would COMMIT and ROLLBACK be called from a php script? (I don't have access to the command line prompt). Would anyone use these for a CMS, intranet etc? I'm trying to learn some best practice techniques.

 

Fenway:

Would you test SQL statements with LIMIT 0 and actually place them in your scripts before calling the insert/update queries? Or would this be more for testing your queries once to make sure they dont return any errors?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.