Jump to content

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


paolinaaaaaaa

Recommended Posts

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
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.