View Full Version : Checking for existing record in MYSQL

01-08-2006, 11:02 PM
Hi everyone

I'm a new user of this site. I'm sorry if this question has been asked before but I had no luck finding the answers.

I've got a mysql database with a single table. I also have a page containing a FORM with a number of fields that once submitted is stored into the database.

My problem is, users are able store the same information more than once. How do i check if that record already exists before inserting into mysql table?

For example, I want to check that, that particular username, nickname and email exists in the table. If it does, print out a warning. If it doesnt exist, go ahead and perform the query and insert the record.

Any help would be much appreciated.


01-08-2006, 11:10 PM
if(mysql_query("SELECT * FROM table WHERE title = `" . $user_suggested_title . "`")) {
die("There is already an entry with this title!");

01-08-2006, 11:21 PM
The previous post's code will return a resource regardless of a match:

if(mysql_num_rows(mysql_query("SELECT * FROM table WHERE title = `" . $user_suggested_title . "`"))>0) {
die("There is already an entry with this title!");

01-08-2006, 11:27 PM
Thanks for the reply guys..

What about if i want to check against more than one field in my table..
For example, i would like to check the following fields exists or not.

firstname, lastname and email

if All the above match, then do not insert.. otherwise go ahead with the insert.

Thanks again in advance

ralph l mayo
01-08-2006, 11:37 PM
if (mysql_num_rows(mysql_query('SELECT COUNT(*) FROM table WHERE firstname=\'' . $firstname . '\' AND lastname=\'' . $lastname . '\' AND email=\'' . $email . '\' LIMIT 1')) == 0)
// insert
// duplicate

01-08-2006, 11:48 PM
Thanks Ralph..
I believe that was the code i was looking for.



ralph l mayo
01-08-2006, 11:50 PM
You're welcome Eddie, good luck.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum