Re: PHP & MySQL get value of auto_increment field of new

From: Date: Thu, 09 Nov 2000 12:41:51 +0000
Subject: Re: PHP & MySQL get value of auto_increment field of new
Groups: php.general 
Request: Send a blank email to php-general+get-24481@lists.php.net to get a copy of this message
Addressed to: "Vijest.com" <slaven.vujic@vijest.com> "Tom Harris" <stry_cat@yahoo.com> php-general@lists.php.net ** Reply to note from "Vijest.com" <slaven.vujic@vijest.com> Thu, 09 Nov 2000 11:43:07 +0100 > > > You can use SELECT count(*). > What if records have been deleted. Count and the highest ID value are not related. > At webpage in PHP code you can use mysql_numrows query, or > last_insert_id, or SELECT id from tablename desc limit 1; > > If two people enter the data at the same time, one of this queires will > communicate with database before second request. > > So, you have no problem at all. I disagree. If one person adds a record, then a second person adds a record, then the first person checks the number of records, then the second person checks the number of records, then you have a problem. Both visitors think they added the second record, and further processing by the first will be done wrong. You may get lucky for a long time, but as hit counts grow, eventually you will get bit by this bug. You should always use mysql_insert_id() to obtain the value of the auto_increment field for the last INSERT query! See: http://www.php.net/manual/html/function.mysql-insert-id.html The manual is your friend... Rick Widmer Internet Marketing Specialists www.developersdesk.com

« previous php.general (#24481) next »