-2

Ok, I'm confused. I have some code that searches a database table for a username, and then uses an if else statement to run some code depending on if the user is found or not. My code is below. The problem is that the code isn't even seeing the if else statement, and I have no idea why. Any help is appreciated.

$sqluser = "select * from users where username='" . $user ."'"; //Searching to see if the user is in the database
echo $sqluser . "<br><br>"; //writes out the select statement to make sure it is correct
$query = mssql_query($sqluser); //returns the results
$num_rows = mssql_num_rows($query); //gets the number of rows returned

echo $num_rows; //writes out the number of rows

if ($num_rows==0) //determines what happens next if the user exists or not
{
    //displays an error box if the user doesn't exist
    echo "<script type=text/javascript>";
    echo "alert('That user doesn't exist. Please try again.')";
    echo "</script>";
}
else
{
    //will be code to run if the user does exist
    echo "<script type=text/javascript>alert('Testing.')</script>";
}
Darth Mikey D
  • 107
  • 2
  • 14
  • is it writing out $num_rows? what is getting printed out? – Icewine Apr 24 '17 at 01:02
  • your query failed and you need to find out why; check for errors – Funk Forty Niner Apr 24 '17 at 01:13
  • It is writing out $num_rows, and the query is working. Even if the query wasn't, wouldn't it have done the else portion? – Darth Mikey D Apr 24 '17 at 01:15
  • Try using `if(count($num_rows))` or `if($num_rows===0)`. – Pyromonk Apr 24 '17 at 01:19
  • Right now there are no records in the database. $num_rows is returning 0. When users are added to the database, it will return either a 0 if the username isn't found, or a 1 if the user is found. I tried if ($num_rows===0), but that didn't make any difference. I'm not sure about using count, as I'm not using an array. Also, when I viewed the page source, the script tags are showing, but for some reason the alert box isn't showing. – Darth Mikey D Apr 24 '17 at 01:40
  • Sorry, should have been more specific. The alert box is showing in the page source, but isn't popping up when I run the code. – Darth Mikey D Apr 24 '17 at 01:44
  • @Fred-ii- Flagged as **Off-topic -> a simple typographical error.** – mickmackusa Apr 24 '17 at 02:10
  • Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Funk Forty Niner Apr 24 '17 at 02:14
  • @mickmackusa I already voted earlier but you can ^ vote on that one; it fits. – Funk Forty Niner Apr 24 '17 at 02:14

1 Answers1

0

I couldn't add a comment. So I will write this as an answer instead.

Since you state that the alert JavaScript is showing in the page source, this mean that the IF/ELSE statement in PHP is working fine. The problem is with the single quote. You have a single quote inside a single quoted alert function. Hence the JavaScript alert function cannot be executed.

echo "alert('That user doesn't exist. Please try again.')";

Try using this instead

echo "alert('That user doesn\'t exist. Please try again.');";