This question already has an answer here:
What is the problem, It is printing the same whatever I do? A help would be appreciated.
<?php
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$connection = mysqli_connect('localhost', 'root', '', 'ayaneshapp');
if($connection) {
echo "Logged in!";
} else {
die("Sorry! Something went wrong.");
}
$query = "SELECT * FROM ayaneshtable ";
$result = mysqli_query($connection, "SELECT * FROM ayaneshtable");
if(!$result) {
die('404 ERROR');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="col-sm-6">
<?php
while($row = mysqli_fetch_row($result)) {
echo $row['username'];
}
?>
</div>
</div>
</body>
</html>
</div>
$result
is null because your code didn't go into the if statement. Every time you visit your page when $_POST['submit']
is not set, it will throw this error.
So to solve it you have to either also wrap your while loop in the same if statement, or make sure that $result
gets filled regardless of there being something in $_POST['submit']
.