0

Im creating a webpage which displays the information from my MYSQL database using php code. However, when I test the page, rather then displaying the information, the code itself appears instead of the information. i cant figure out why such is happening as i have used the code before with no problem.

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Game Portal</title>
        <style>
        //CSS CODE  
        </style>

    </head>
    <body>
            <div class="right"></div>
            <div class="center">
                <?php
                    $table_name = "";
                    $host_name = "";
                    $user_name = "";
                    $password = "";
                    $db_name = "";
                    $db_link = mysqli_connect($host_name, $user_name, $password, $db_name) or die(mysqli_error($db_link));
                    $query = "select Name, Cover from $table_name;"; 
                    $result = mysqli_query($db_link, $query) or die(mysqli_error($db_link));
                    echo "<table>";

                         while ($row = mysqli_fetch_array($result))  {
                                     echo "<tr><td>{$row[0]}</td>";
                                     echo "<td align=right>{$row[1]}</td></tr>";
                                }

                        echo "</table>";
                ?>
            </div>
        </div>
        <div class="footer">
        </div>
    </body>
</html>

Note that i removed bits of the code that aren't needed for the php part in order to make it easier to read.

7
  • Is your file .html or .php? .html files won't be parsed by PHP unless you specifically set them to. Commented Mar 4, 2015 at 2:42
  • its html, how would i make it parse php? Commented Mar 4, 2015 at 2:42
  • Change extension to .php would be easiest way. Commented Mar 4, 2015 at 2:43
  • ok, but id like to keep the file as an html Commented Mar 4, 2015 at 2:44
  • @A22asin you don't want to change the extension for SEO purpose or for another reason? Commented Mar 4, 2015 at 3:01

3 Answers 3

1

Create a .htaccess

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

Or

<FilesMatch "\.html$">
    ForceType application/x-httpd-php
</FilesMatch>

PHP File

enter image description here

Browser

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Seeing as your using .html files, if you really want to parse them as PHP (I wouldn't recommend it as if they don't contain PHP code, they will still be parsed adding overhead)

Add AddType application/x-httpd-php .html to your .htaccess (assuming your using Apache)

Comments

1

You can rewrite requests for (.*).html into a matching .php file. Pop the following into a .htaccess file in the directory where your wares are:

RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php

The page will then be visible as .html to your users, but obviously your PHP code gets processed. Presuming you have Apache as the server you use.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.