Jump to content


Photo

PHP/MySQL unexpected results


  • Please log in to reply
3 replies to this topic

#1 pcwood

pcwood
  • New Members
  • Pip
  • Newbie
  • 5 posts
  • LocationDelaware

Posted 12 October 2006 - 07:41 PM

Greetings,

I'm using a familiar formula to retrieve and check results from a mysql db.  However, I'm stuck on a response I haven't seen before.  Perhaps someone can tell me why I'm getting the response I am.  TIA:


<?PHP
// if the user hit the submit button and filled the email field then ...
if(isset($_POST['submit']) && $email != "") {
 
  // Check db for existence of user
  $link = mysql_connect ("localhost", "the_admin", "my_password");
  if(!$link) {
    echo "Database Connect Error: " . mysql_error() . "<BR>Please contact our company or try back later.";
  }

  // Select the database
  $selectResponse = mysql_select_db ("the_database", $link);
  if(!$selectResponse) {
    echo "Database Select Error.  Please contact our company or try back later.";
  }
 
  // Make the SQL statement call
  $query = sprintf("SELECT Cemail, Ccqrt_question, Ccqrt_answer FROM customer_data WHERE Cemail='%s'", mysql_real_escape_string($email));
  $result = mysql_query($query, $link);
  echo "Query: $query<P>\n";
  echo "Result: [$result]<BR>Error:" . mysql_error() . "<P>";
 
  if(!$result) {
    echo "Customer [$email] was not found to be on record.  Please check your spelling and try again or ";
echo "<A HREF=\"custREG.htm\">create a new account</A>.  Thank you.";
  } else { 
    $row = mysql_fetch_row($result);
    $Cemail = $row[0];
$Ccqrt_question = $row[1];
$Ccqrt_answer = $row[2];
echo "Security Question: $Ccqrt_question<P>\n";
echo "<INPUT TYPE=\"text\" SIZE=\"50\" MAXLENGTH=\"50\" NAME=\"answer\"><P>\n";
echo "I cannot remember this either.  Please just reset my password and email it to me.\n";
  } 

  mysql_close($link);
 
} else {
  echo "Enter your username (email address)<BR>\n";
  echo "<INPUT TYPE=\"text\" SIZE=\"50\" MAXLENGTH=\"40\" NAME=\"email\"><BR>\n";
  echo "<INPUT TYPE=\"reset\" NAME=\"Reset\" VALUE=\"Reset Form\">&nbsp;\n";
  echo "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"submit\">\n";
}
?>


NOTE: This is, of course, incomplete and I have ECHOs embeded to help me figure what's going on.  Here is what I get when I put either a good or or bad query in:


Query: SELECT Cemail, Ccqrt_question, Ccqrt_answer FROM customer_data WHERE Cemail='s.austin@megabucks.com'
Result: [Resource id #2]
Error:


I've done this many times before without fail.  Can anyone see the problem?
---
Paul Wood

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 12 October 2006 - 08:03 PM

Your assumption that $result is false if there are no records found is causing the problem. Not finding any records which match the WHERE criteria is not an error therefor $result will still have a valid resource value.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 pcwood

pcwood
  • New Members
  • Pip
  • Newbie
  • 5 posts
  • LocationDelaware

Posted 14 October 2006 - 04:30 AM

So shall I assume $result ==0 is the point of failure?  Does this ONLY occur with the WHERE clause?  If so, why?

Example Code from php.net looks like this:

<?php
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
   die('Invalid query: ' . mysql_error());
}

?>


How have I changed the outcome?

Thanks for the response.

-PCWOOD
---
Paul Wood

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 14 October 2006 - 07:59 AM

$result = mysql_query('SELECT * WHERE 1=1');

That query returns "false" because it has a syntax error (no FROM clause)
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users