Naithin Posted July 30, 2007 Share Posted July 30, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/62555-phpsql-verify-data-exists/ Share on other sites More sharing options...
Naithin Posted July 30, 2007 Author Share Posted July 30, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/62555-phpsql-verify-data-exists/#findComment-311412 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.