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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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