Jump to content

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


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.

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.