Question about mysql_fetch_array

When I run the following code, and sendMatches is set to yes in the database, the “Stopped: All Notifications” message correctly displays. However, if I reload the webpage when sendMatches is set to no in the database, the “Oooops” message does not display. Any ideas on what I’m overlooking?

Thanks!

// Does sendMatches already = “No”?

$alreadyLogged = “SELECT lastLogin FROM users WHERE uID = ‘$_SESSION[uID]’ and sendMatches = ‘No’”;

$result = mysql_query ($alreadyLogged) OR die("<b>A fatal MySQL error occurred</b>.
<br />Query: " . $alreadyLogged . “<br />
Error: (” . mysql_errno() . ") " . mysql_error());

if ($row = mysql_fetch_array($result)) {

// Display text to member

$pageHeader = “Oooops”;
$pageIntro = “<p>You have already indicated that you are no longer interested in maintaining a membership on our website. We made note of your decision in your account.</p>”;

} else {

// Display text to member

$pageHeader = “Stopped: All Notifications”;

$pageIntro = “<p>Thank you for letting us know. You will no longer receive search match notifications for ANY of your saved searches. If this was done by mistake, you can turn the notifications back on by going into <a href = ‘myInfo.php’>My personal info</a>.</p>”;

Hey,

What does show, the stopped message again?

You might be better looking at mysql_num_rows to check how many results were returned.

Can you try this test instead?

I cannot test this as I do not use mysql_* functions, but I think it returns false if nothing matching is found. fetch_row at least only tries to get 1 row …


if (mysql_fetch_array($result)) { 
//something was found
}else{
//nothing was found

}

You should seriously be moving away from the mysql_* extension and over to either the mysli_* (MySQL Improved) extension or PDO. The mysql_* extension is deprecated as of the current version of php and will highly likely be removed from the next version of PHP

Try adding these lines, run the script then refresh the page, spot the difference then test for the change in your if(…) statement:



$alreadyLogged = "SELECT lastLogin FROM users WHERE uID = '$_SESSION[uID]' and sendMatches = 'No'";

$result = mysql_query ($alreadyLogged) 
             OR 
             die
            ( "<b>A fatal MySQL error occurred</b>.\
<br />Query: " 
                 . $alreadyLogged 
                 . "<br />\
Error: (" . mysql_errno() . ") " 
              . mysql_error()
            ); 

// ADD THESE LINES - start ==================================================
if(true): # toggle with false once problem solved
  echo $alreadyLogged;
  echo '<pre>';
    $row = mysql_fetch_array($result);
    var_dump( $row );
  echo '</pre>';
  die;
endif;
// ADD THESE LINES: finish ==================================================

 if ($row = mysql_fetch_array($result)) {
...
...
...