0

Is it possible that a php script gets terminated between two mysql queries.

For example user registration:

1st INSERT : I will enter the basic first-name,last-name,address in one table

and

2nd INSERT : i will enter the user's hashed password and salt in another table

This operation probably requires two queries and either of them independently are useless records.

What if the php script terminates after executing the first query? User will just get a server error message but one useless record will be generated.

Any Solutions??

EDIT ------ My Web host does not provide a InnoDB engine. Only MyISAM supported

10
  • 3
    You should look into transactions. Commented Jul 26, 2013 at 19:41
  • Note that the database engine must support transactions: MyISAM doesn't, InnoDB does Commented Jul 26, 2013 at 19:46
  • My web host doesn't provide a InnoDB Engine and that's the major reason for this problem....Any solutions for MyISAM Commented Jul 26, 2013 at 19:48
  • @user2439376 well, than you should make transaction table yourself, but this will be ugly Commented Jul 26, 2013 at 19:53
  • by transaction table i mean, that you insert record for each step, and before second query you verify that all previous steps are succeed Commented Jul 26, 2013 at 19:55

2 Answers 2

3

Use a transaction:

START TRANSACTION;
INSERT INTO foo ...
INSERT INTO bar ...
COMMIT

If either INSERT fails, you ROLLBACK the transaction and you won't be left with "useless" records.

Sign up to request clarification or add additional context in comments.

1 Comment

Then you'll have to simulate a rollback. try to insert the first record. try to insert the second record. if either insert fails, delete the relevant row from the other table.
0

if you use a transaction such as:

BEGIN;
INSERT INTO TBL(...) VALUES(...);
INSERT INTO TBL(...) VALUES(...);
COMMIT;

Everything is sent to the mysql server in one go, and then ran as a batch, meaning your script can terminate as soon as the transfer is complete, rather than waiting for each individual query. It also has the added bonus of using less ram and being much faster.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.