Jump to content

Recommended Posts

Hi guys,

 

I've ran into a problem which has me.. stumped.. To say the least. The problem at the moment is simply getting my code to verify whether or not data exists, and act accordingly.

 

Problem block of code must lie here somewhere:

 

$check = mysql_query("IF EXISTS (SELECT 1 FROM `pilots` WHERE `pilotid` = '$_id') RETURN 1 ELSE RETURN 0",$db);

if ($check == 0) // Pilot is NEW, INSERT.
	{
// Do Stuff, run INSERT, etc.
	}
if ($check == 1) // Pilot EXISTS, UPDATE.
        {
       // Do Stuff, run UPDATE, etc.
        }

 

The main problem being that $check doesn't actually seem to return anything. If I echo it it comes up as nothing. However I can run both my update and insert code perfectly if I artifically change the value of check to the right one. So the error is definitely in my validation checking, just not quite sure how to correct it. If you can spot any problems, please let me know! :)

 

Link to comment
https://forums.phpfreaks.com/topic/62555-phpsql-verify-data-exists/
Share on other sites

Well, I've managed to get what I want done with a different SQL query, current verification code is:

 

$check = mysql_query("SELECT 1 FROM `pilots` WHERE `pilotid`='$_id'",$db) or die ('Error: ' . mysql_error());
$res = mysql_numrows($check);

if ($res == 0) // Pilot is NEW, INSERT.
{
	// Do stuff
}
if ($res == 1) // Pilot EXISTS, UPDATE.
{
	// Do stuff
}

 

But I have heard the SELECT method has a lot more overhead than an IF EXISTS setup. Is this true? Is what I have an acceptable practice to get the information I want?

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.