I'd like to query a mysql database for a single field ["notice"] and display the result in an SHTML page. The query will never return more than one row back and it only returns one column. If there's something in the row, then I'd like to show some text and a <br>. If the Select statement returns nothing, then I'd like to display nothing, not even the <br>.
I have the following code that works fine as a test when it's saved as a PHP file. However, If I copy everything inside the <body> tags and insert it into an HTML page body, the last half of the code is displayed. It displays everything after the greater than symbol in the IF statement as text. Similarly, if I save this code as an HTML or SHTML file instead of a PHP file, I display the last half of the code in the browser. How do I include the <body> tag on an HTML page?
Thanks for looking at this.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT notice from notification where page = 'home'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Message is: " . $row["notice"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</body>
</html>