0

I think this should work but it is not...

Basically i am trying to check mysql db to see if there is a record that meets the 2 variables..if no do one thing if yes do another thing. the result is always no at this point.

$result = mysql_query("SELECT 'lastname' FROM 'Cust_Releases' WHERE 'lastname' = '$usercheck' AND          'TripID'= '$RLtripid'");
echo $result;
if(mysql_num_rows($result) == 0) {
 echo"no";// row not found, do stuff...
} 
else {
 echo"yes"; // do other stuff...
  }
Dean
  • 1
  • 1
  • 3

4 Answers4

1

First of all, stop using mysql_* functions because this extension is deprecated as of PHP 5.5.0.

Second always use the (`) symbol around database names, table names and column names.

You have a reserved word used RELEASE.

$sql = "SELECT `lastname` FROM `Releases` WHERE `lastname` = '$usercheck' AND `TripID` = '$RLtripid'";

Reserved words you find here

Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
  • 100% agree with the first. Disagree with the second. These are only *required* when you conflict with reserved words. So, better yet, always avoid using reserved words - http://stackoverflow.com/questions/5952677/should-i-use-backticks-or-not-when-escaping-keywords-in-mysql – Jason McCreary Jan 16 '13 at 16:02
  • This should be a comment as there doesn't seem to be anything that solves OPs question – andy Jan 16 '13 at 16:03
  • thanks for the quick responses... there isone record in the db and i can echo the column lastname and it prints the record thatis in the db – Dean Jan 16 '13 at 16:08
  • now i have this error and the answer is still always no. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in... – Dean Jan 16 '13 at 16:16
  • one variable contains a lastname and the other is a number – Dean Jan 16 '13 at 16:23
1
$result = mysql_query("SELECT lastname FROM `Releases` WHERE lastname = '$usercheck' AND TripID= '$RLtripid' LIMIT 1");
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
echo $result;
if(mysql_num_rows($result) == 0) {
 echo"no";// row not found, do stuff...
} 
else {
 echo"yes"; // do other stuff...
  }
  • Escaping 'Releases', as Bondye suggested
  • Adding 'LIMIT 1' to your query to allow the possibility of an early-out when there is more than one matching record. You don't appear to need the total count. May not make any difference if unique constraints exist which guarantee that only one row can be returned
  • mysql_query is deprecated. In real code you should be using PDO and prepared statements / bind variables!
Jason
  • 11
  • 1
0

debugging is a very important thing in programming. first do make sure that the varibales $usercheck, and $RLtripid contain values.

-----------------------
$sql = "SELECT `lastname` FROM `Cust_Releases` WHERE `lastname` = '$usercheck' AND `TripID`= '$RLtripid'";
echo $sql;
$result = mysql_query($sql);
....-------------------
Alsaket
  • 175
  • 1
  • 11
  • Yes i understand and i can echo those individually and see the proper results. However if i echo my $Result i get resource id #19. – Dean Jan 16 '13 at 17:14
-1
Try this code. It will help you

$result =  mysql_query("SELECT COUNT( * ) from Cust_Releases lastname = '$usercheck' AND TripID= '$RLtripid'");
if($result  == 0) {
 echo"no";// row not found, do stuff...
} 
else {
 echo"yes"; // do other stuff...
  }
Ankit Agrawal
  • 6,034
  • 6
  • 25
  • 49