-2

I have made a login form using PHP and MySQL. However, I am running into an error when I type in the wrong login info.

<?php

    while($row = mysql_fetch_array($result))
    {
    if($result)
      {
       header('Location: supplementsstore.php');
      }
    else
      {
        header('Location: logon.php');
      }
    }

?>

The if works as it's supposed to, but it does not do the else statement. I have tried changing else to !$result but that didn't work either.

halfer
  • 19,824
  • 17
  • 99
  • 186
AndyCon
  • 1
  • 2
  • 3
    *"i am running in to an error"* - Being? Take your pick of 1001 possible reasons. *Totally unclear what you're asking*. – Funk Forty Niner Apr 03 '15 at 18:15
  • 1
    Why are you doing `if($result)`? – gen_Eric Apr 03 '15 at 18:15
  • What is $result we need more details – TheSk8rJesus Apr 03 '15 at 18:16
  • Whatever your mistake is, the `exit` is missing for guarantee. – hakre Apr 03 '15 at 18:19
  • If you are inside that loop, that already means if($result) is true, so your else statement is unreachable. – Dan Apr 03 '15 at 18:19
  • I am not an expert at PHP, so correct me if I am wrong, but wouldn't `if($result)' just test if `$result` was not null. Try extracting the data from the SQL result, and comparing it against user input – James Parsons Apr 03 '15 at 18:30
  • `(null !== $result)` tests if `$result` is not `null`. `if ( ... )` just checks for something being truthy (`null` isn't truthy but falsy in PHP so you're not far off with the `null` example). – hakre Apr 03 '15 at 18:48
  • Do the comparison against the database and store the result of the comparison into another variable. Then *after* the comparison place the if. That should help you keep things separated which will make it more easy for you. – hakre Apr 03 '15 at 18:50

2 Answers2

2

If if($result) were to evaluate to false the while loop would never be entered.

So try:

if( count($row) > 0 ) 

rather than

if($result){}
Dan
  • 10,614
  • 5
  • 24
  • 35
user2478240
  • 369
  • 3
  • 15
  • 1
    I'd never take that stab in the dark, even with the biggest and longest knife around. But, you may be right. Let's see what the OP has to say ;-) Could be a lucky shiv. – Funk Forty Niner Apr 03 '15 at 18:18
0
if($row = mysql_fetch_array($result))
{
header('Location: supplementsstore.php');
}
else
{
header('Location: logon.php');
}
iguypouf
  • 770
  • 4
  • 15