0

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>

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for responding. I'm on a shared server (HostGator). Is there anything special I have to do, or do I just save the three lines above as the .htaccess file
@user3138025 Save the three lines in your .htaccess file. If that gives you a 500 error, then try removing the 5 after php, if you're still getting a 500 error, ask tech support what the right format for your server is.
@user3138025 Glad to have helped. Please click the checkmark below the arrows on the left of this post to indicate this solved your problem.
Hello,Thanks again for responding so quickly. the change to .htaccess made my PHP work, but it disabled my Include statements. For instance, I have a few places in my page like "<!--#include virtual="menu_primary.shtml" -->". That statement doesn't seem to work any longer when I add the <FilesMatch tag. My .htaccess file looks like: Options +FollowSymLinks <FilesMatch "\.(htm|html|shtm|shtml)$"> SetHandler application/x-httpd-php5 </FilesMatch>
Hi David, Thanks. The following worked well for the include: <?php include("menu_primary.shtml"); ?> Thanks!
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.