Re: error returning with PEAR::DB

From: Date: Wed, 31 Mar 2004 12:23:21 +0000
Subject: Re: error returning with PEAR::DB
References: 1 2  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-11555@lists.php.net to get a copy of this message
Martin Marques wrote:
On Tue, 30 Mar 2004, Pupeno wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I have a database which has a unique key that is chosed by the user (the username), so, I should complain to the user if the username is already used and prompt for choosing another one... ok. I could check if that username was used by running a query... But I'm trying an alternate way to do it, I want to directly insert the new username and if it fails check why it failed and if it was the username, ask for a diferent username.
That's the wrong way to proceed. If you have a UNIQUE key, you first have to verify that the values you are inserting are valid ones: SELECT * FROM tabname WHERE unique condition Then verify that there are no rows to fetch with $res->numRows() and there you can do the insert, or through a message to the user asking for a different value.
and then you have to make the entire operation atomic, since otherwise someone could insert something inbetween your select and your insert. the database constraints are more than a last barrier to failure in my eyes. they also assist in the functinality of the system. but now we are getting into philosophie regards, Lukas Smith smith@backendmedia.com _______________________________ BackendMedia www.backendmedia.com berlin@backendmedia.com Linn Zwoch Smith GbR Pariser Str. 44 D-10707 Berlin Tel +49 30 83 22 50 00 Fax +49 30 83 22 50 07

« previous php.pear.general (#11555) next »