Jump to content

[SOLVED] Checking if a record exists in MYSQL Database?


ravix76

Recommended Posts

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

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.

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.