Reputation: 825
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>
Upvotes: 0
Views: 134
Reputation: 1
PHP include and require Statements It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.
The include and require statements are identical, except upon failure:
require will produce a fatal error (E_COMPILE_ERROR) and stop the script include will only produce a warning (E_WARNING) and the script will continue So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.
Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
Upvotes: 0
Reputation: 4883
You'll need to use .htaccess to run your file through the PHP parser. Something similar to:
<FilesMatch "\.(htm|html|shtm|shtml)$">
SetHandler application/x-httpd-php5
</FilesMatch>
Upvotes: 2