ravix76 Posted March 11, 2007 Share Posted March 11, 2007 Can anyone tell me the best way to find out if a Record already exists in the Database. I'm using the following Code 27 $results = mysql_query("SELECT product FROM `products` WHERE `brand` = '$currentbrand' AND `product` = '$newproductname'"); 28 $nameexists = mysql_result ( $results,0 ); 29 if ($nameexists == $newproductname) $exists=1; However, If the product doesn't exist, the script returns No Rows and and displays a warning "Unable to jump to row 0 on MySQL result index 4" which in turn, I think (?) is causing a Header Error in Line 28. Object Buffering will work around it, but is there a more efficient method? Thanks Link to comment https://forums.phpfreaks.com/topic/42229-solved-checking-if-a-record-exists-in-mysql-database/ Share on other sites More sharing options...
cmgmyr Posted March 11, 2007 Share Posted March 11, 2007 You can use: $results = mysql_query("SELECT product FROM `products` WHERE `brand` = '$currentbrand' AND `product` = '$newproductname'"); $count = mysql_num_rows($results); if($count>0){ echo "You have a record"; }else{ echo "You do NOT have a record"; } Hope this helps. Link to comment https://forums.phpfreaks.com/topic/42229-solved-checking-if-a-record-exists-in-mysql-database/#findComment-204856 Share on other sites More sharing options...
ravix76 Posted March 11, 2007 Author Share Posted March 11, 2007 Great. Works a treat. Thanks! Link to comment https://forums.phpfreaks.com/topic/42229-solved-checking-if-a-record-exists-in-mysql-database/#findComment-204946 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.