Tuesday, 23 May 2017

mysql - PHP no error detected but not showing properly




I am new to PHP and I have a file named Login.php and the code goes like this:





if(!isset($_POST['submit']))
{?>

Username:
Password:


Not Yet Registered? Register here.

}
else
{
require_once("db_const.php");
$mysqli = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if($mysqli->connect_errno)

{
echo "MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_errno}";
exit();
}
$username = $_POST['username'];
$password = $_POST['password'];

$sql = "select * from user where username like '{$username}' and password like '{$password}' limit 1";
$result = $mysqli->query($sql);
if($result->num_rows == 1)

{
echo "Invalid username/password";
}
else
{
echo "Login Success!";
}
}
?>





but when I run this in the browser it looks somewhat like this:



Username: box
Password: box
button Login Not yet Registered? Register here. ("here" is a link)



connect_errno) { echo "MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_errno}"; exit(); } $username = $_POST['username']; $password = $_POST['password']; $sql = "select * from user where username like '{$username}' and password like '{$password}' limit 1"; $result = $mysqli->query($sql); if($result->num_rows == 1) { echo "Invalid username/password"; } else { echo "Login Success!"; } } ?>



Did I miss something or my php version is not compatible? (version 5.5.9)

I use xampp by the way.


Answer



It looks like the script is not being interpreted as PHP. If you check the browser source (ctrl-u/cmd-u) you will see the whole contents of the PHP file sent to the browser. (The > of $mysqli-> is interpreted as the end of the tag)



So you need to check your web server to make it interpret PHP. Are you accessing the file over HTTP? (see this SO answer)



After all you should verify that PHP is running by opening a simple script like this:






No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...