Use PHP to See if a MySQL Record Exists

Alles, was PHP betrifft, kann hier besprochen werden.

Use PHP to See if a MySQL Record Exists

Postby acherman » 05. August 2010 23:55

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.

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. Using other resources my current PHP statement looks like this:

Code: Select all
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
User avatar
acherman
 
Posts: 1
Joined: 05. August 2010 23:41

Re: Use PHP to See if a MySQL Record Exists

Postby Altrea » 06. August 2010 06:46

acherman wrote:Hey Everyone,

Hi,

acherman wrote: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.

Thanks a lot :D

acherman wrote:My current trouble is getting PHP to return the proper values for the SQL query.

No. MySQL and PHP returns the correct values for your SQL-query. IT's the SQL Query which fails.

acherman wrote:
Code: Select all
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';}


Don't use ' (single quotes) for your Column-Names. Use backticks => ``

btw: You should better code with error handling. Something like this:
Code: Select all
<?php
// your DB connect here

$sql = "SELECT * FROM `users` WHERE `site_name` LIKE 'berland' AND `card_id` LIKE '290093C84E' LIMIT 0 , 30";
//echo $sql;

$result = mysql_query($sql);
if(!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $sql;
    die($message);
}

$num_rows = mysql_num_rows($result);
//echo $num_rows;

if($num_rows > 0) {
    echo '1';
} else {
    echo '0';
}


This way you can easily debug your DB-query with echoes and mysql_error().

acherman wrote:Any help is appreciated.

- Are you sure, that you want to use "LIKE" instead of "="?
- Do you need the LIMIT?
- Don't use * unless you really need ALL table-columns
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11935
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64


Return to PHP

Who is online

Users browsing this forum: No registered users and 51 guests