Jump to content


Photo

How to check if a tuple is already in a mysql database with php


  • Please log in to reply
2 replies to this topic

#1 paolinaaaaaaa

paolinaaaaaaa
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 10 July 2006 - 04:01 PM

Probably a really stupid question but I am a total beginner ???...so here is the scenario:

I have a html form in which people can insert a series of values.
On pressing submit these values should be used to insert a tuple in a mysql database using php.
However the values could already be in the database.
I therefore need to check that beforehand.
In short I need to do:

IF (tuple already in db)
return tuple primary key
ELSE
insert tuple in db
return tuple primary key

how could I do this?
I have tried doing a select and seeing if it returns false but either I get a value (if the tuple is in the db) or I get an error (if the tuple is not in the db) and my script doesn't reach the end.

Thank you very much,

Paolina

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 July 2006 - 04:54 PM

There are a few ways you can do this.  However, no select query should ever return an error, just an empty result set, a boundary condition that you can easily check.  Anyway, you could always do an INSERT IGNORE, provided you have an appropriate unique key across the relevant column; alternatively, you could do the select, and if no result, then insert, and pull back the last_insert_id.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 paolinaaaaaaa

paolinaaaaaaa
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 13 July 2006 - 12:44 PM

thanks :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users