Jump to content

Use PHP to See if a MySQL Record Exists - Return Only True or False


acherman

Recommended Posts

Hey Everyone, I want to start out by saying thanks in advance for reading this. Since I started working with the XAMPP package this community has been a great resource, though this is my first post.  most of my work has been modifying existing stuff in our network, this is my first project from scratch.

 

I am working on an access control project where I am storing site names and RFID tags in a table, and using PHP to check for existing records. Basically, I want to submit 2 credentials from a host (site and RFID), check if they exist together in the table, if so return TRUE or 1, if not return FALSE or 0. I am new to both PHP and MySQL (although I took a course years ago) so I am looking here for help.

 

On this server I have XAMPP installed and everything I need running. I have a table and created a few records for testing. My current trouble is getting PHP to return the proper values for the SQL query. The SQL query works fine, I think - it returns the number of rows that match and that record.  I'm not sure this is the result I need to be able to deal with the way I want.  Using other resources my current PHP statement looks like this:

if(mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE 'site_name' LIKE 'berland' AND 'card_id' LIKE '290093C84E' LIMIT 0 , 30"))>0){echo '1';}
   else
   {echo '0';} 

So, my table has the entry as noted in this query above. Final one will be dynamic where the host will be sending those values - I will likely need help with that as well, but small steps first.;-) Anyway, I was playing around with the end statement >0{echo '1';} but wasn't able to make it do what I want.

Any help is appreciated. Thank you.

 

Aaron

Link to comment
Share on other sites

I believe there is a problem with you SQL query - The column names are enclosed with the character ' and really should be with the character `.

 

Also while calling mysql_query within mysql_num_rows or mysql_fetch_assoc looks more efficient it makes it harder for Debugging.

 

I suggest something to the following.

 

$sql = "SELECT * FROM `users` WHERE `site_name` LIKE 'berland' AND `card_id` LIKE '290093C84E' LIMIT 0 , 30";
$res = mysql_query($sql) or die(mysql_error() . "<br/>" . $sql);
if(mysql_num_rows($res) > 0)
     echo '1';
else
     echo '0';

 

That way you can also use the same result to iterate through the rows.

Link to comment
Share on other sites

First, thanks for the response and your help.  Much appreciated.

 

I tried your query, and I must have done something wrong because it didn't work.  Then I copy and pasted and all is good.  You are my hero!!  :thumb-up: Now I need to sort out the rest of plan.

 

Thank once again!!!

Link to comment
Share on other sites

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.