|  | Posted by Andy Hassall on 06/25/05 16:12 
On Fri, 24 Jun 2005 08:54:33 -0400, "matt" <guest@guest.com> wrote:
 >Hey everyone.  I've been doing some php/mysql programming for a while, but i
 >don't know if i've been doing this the correct way.  if i have a table that
 >i would like to know if it contains 1 particular record, what is the best
 >way of doing it?  There should not be more than one instance of it, and it
 >could only be in one row.  Specifically, i want to know if an email address
 >is in the database.
 >
 >I've been doing a "SELECT * FROM email WHERE address = '$address'"; and then
 >using mysql_num_rows() to see if there if it is greater than zero.  is there
 >a command to see if that address exists, such as returning TRUE or FALSE.
 
 That's already a reasonable approach, and you won't get very much shorter than
 that. Another one is to "select count(*) ..." and fetch the single row it'll
 return - that'll be either 0 or 1 which you can use directly in PHP as 0 ==
 false, 1 == true.
 
 What are you using this test for? If you're checking if an email address is
 already in the table immediately before doing an INSERT, then you should
 instead use a unique constraint on the table and let MySQL handle it - this
 avoids race conditions, and is quicker since it eliminates a roundtrip to the
 database.
 
 --
 Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
 <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
  Navigation: [Reply to this message] |