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

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.

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!!!

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.