To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 09-30-2003, 01:21 PM   #1
ldd76
Junior Member
 
Join Date: Aug 2003
Posts: 28
Concurrent MySQL access from PHP

Hi gurus,

I am quite new to MYSQL. I would like to allow concurrent access to my leave application.
Not sure how to the code is to be done but according to some MySql docs...is this correct?


$db = mysql_connect("localhost","root","root");

if(!$db){
print("Could not connect to database. Please try again later.");
exit;
}

mysql_select_db("lv_mgmt_sys");

$query = "SET AUTOCOMMIT = 0";

mysql_query($query);

$query = "START TRANSACTION";

mysql_query($query);

$query = "SELECT * FROM superv_details WHERE (emp_id='$emp_number')";

mysql_query($db_query);

$query = "COMMIT";

mysql_query($query);

$numrows = mysql_num_rows($result);

if($numrows > 0){
$query = "UPDATE superv_details SET superv_name='$fullname',dept_code='$department',emp_status='$emp_status',email='$email_addr' WHERE emp_id='$emp_number'";
}

mysql_query($query);

$query = "COMMIT";

mysql_query($query);

Can someone give me an example of how it should be done?

Thanks
ldd76 is offline   Reply With Quote
Old 09-30-2003, 05:37 PM   #2
steadyguy
Senior Member
 
steadyguy's Avatar
 
Join Date: Jan 2003
Location: Brazil
Posts: 344
Pretty sure I don't fit into the 'guru' category, but it occurs to me that this doesn't seem like a situation which requires transactions: because you want to execute the SELECT query all the time, and not have it fail if the UPDATE fails. Instead, I would simply do this:
PHP Code:
$query = "SELECT * FROM superv_details WHERE (emp_id='$emp_number')";
$res = mysql_query($query);
if(
$res) { //because mysql_query() will return FALSE if a SELECT query has an empty result set (or on error)
    
$query = "UPDATE superv_details SET
        sperv_name='$fullname', dept_code='$department', emp_status='$emp_status', email='$email_addr'
        WHERE emp_id='$emp_number'"
;
    
mysql_query($query);
}
However, if you really need to ensure that no one is reading from the table while the script is running, you can obtain a lock which is more portable than a transaction - because it works with all MySQL table types.
PHP Code:
// ... same as above ...
if($res) {
    
mysql_query('LOCK TABLES superv_details WRITE'); //now no one can read OR write from the table until it is unlocked.
    
if (!mysql_error()) {
        
$query = "UPDATE superv_details SET
            sperv_name='$fullname', dept_code='$department', emp_status='$emp_status', email='$email_addr'
            WHERE emp_id='$emp_number'"
;
        
mysql_query($query);
        
mysql_query('UNLOCK TABLES'); //unlock tables, don't forget this!!
    
}
    else {
        print
mysql_error();
        exit;
    }
}
steadyguy is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 10:03 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.