Transactions in MySQL using PHP

This is a discussion on Transactions in MySQL using PHP within the PHP Language forums, part of the PHP Programming Forums category; I am a general user of PHP and Mysql, not an expert. But I am having following problem: we have ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-17-2005
Abhi
 
Posts: n/a
Default Transactions in MySQL using PHP

I am a general user of PHP and Mysql, not an expert. But I am having
following problem:
we have two tables called
Users(uID, uName)
Users_Groups (gID, uID, isAdmin)
uID is Autoincreament in Users and Foreign key in UserGroups table.
I am using transactions to insert a new user into User Table and
immediately insert into Users_Groups during the same transaction. It is
an atomic operation. This happens in the same PHP script. I tried
using use mysqli and old mysql_ calls. This transcation works well when
I execute in an IDE like mysqlControl Center. But in the PHP script.
It does not go through Here is the PHP Pseudocde
Begin
SET FOREIGN_KEY_CHECKS = 0
Insert into Users Table
If (sucessful) Then
Retrieve userid = mysql_insert_id

Insert into Users_Groups values(gID, userid, 0)
if (sucessful) Then
COMMIT
else
ROLLBACK
SET FOREIGN_KEY_CHECKS = 1
In this case I am able to retrieve and print the inserted
mysql_insert_id for Users table but when I try to insert it into the
Users_Groups table it fails and ROLLS-BACK. My guess is it fails the
Foreign key constraint even though I SET FOREIGN_KEY_CHECKS = 0.
Any ideas why this may be happening? Please let me know.
Thank you in advance,
Abhi

Reply With Quote
  #2 (permalink)  
Old 05-17-2005
jabhijit@gmail.com
 
Posts: n/a
Default Re: Transactions in MySQL using PHP

I am sorry to post this but I found the bug in my code. The above logic
works well.
It was basically the problem of using mysql_insert_id for a table that
does not have an autoincreament column in order to find out if
insertions worked or not. Instead using mysql_affected_rows() fixed
the problem. Fresh mind in the morning
helped looking at the problem.
Abhi

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:56 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0