If page name doesn't exist then I would like to show page not found
instead of a blank white page.
.htaccess:
RewriteEngine On
RewriteRule ^([0-9a-z\-\_]+)?$ page.php?name=$1 [L,QSA,NC]
page.php:
$name = filter_input(INPUT_GET, 'name');
$result = $mysqli->query("SELECT * FROM pages WHERE name='$name'");
while ($row = $result->fetch_assoc()) {
echo $row['content'];
}
If some of you know a better way, please share it :)
You can change your PHP code like this:
$name = filter_input(INPUT_GET, 'name');
if ($result = $mysqli->query("SELECT * FROM pages WHERE name='$name'")) {
if ( $result->num_rows == 0 ) {
printf('404 / page Not Found');
exit;
}
else {
while ($row = $result->fetch_assoc()) {
echo $row['content'];
}
}
} else {
printf("Error: %s
", $mysqli->error);
exit;
}
You can get the mysql num rows and make if it == 0 then show page not found before the while loop