0

Learning PHP and MySQL. Made a short script that retrieves contents from the database, and populates it in a tag in my html header.

index.php:

<?php

$link = mysqli_connect('localhost', 'root', 'abdullah');

if (!$link) {
    echo "Could not connect";
    exit();
}

if (!mysqli_select_db($link, 'chitra')){
    echo "Could not find database";
    exit();
}

$query = 'SELECT * FROM photos;';
$result = mysqli_query($link, $query);

while ($row = mysqli_fetch_array($result)){  
  $categories[] = $row['category'];
}

include 'main.html';

?>

main.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Chitra</title>
        <link rel="stylesheet" type="text/css" href="index.css">
    </head>
    <body>

        <header>
            <div id="header_username">username</div>
            <div id="header_cat">
                <select>

                    <?php foreach ($categories as $category): ?>
                    <option><?php echo htmlspecialchars($category, ENT_QUOTES, 'UTF-8'); ?></option>
                    <?php endforeach; ?>

                </select>
            </div>
        </header>
        <main>
          TODO
        </main>
    </body>
</html>

This code was initially working. I started working on the body for a while, until suddenly, it started returning me a blank white page. I removed my main content to start over from scratch, but it still give me the same result.

I put echo statemnts throughout the PHP file, everything is executing fine until right before the include statement. I checked my Apache, MySQL, PHP installations, checked that everything is up and running. Everything is. I can't figure out the issue.

  • What is the HTTP response? Have you checked your PHP/fcgi/apache logs? Anything interesting? How about your database? Is the data still in there? Have your paths changed? Is your script looking for `main.html` in the right place? –  Feb 13 '18 at 06:50
  • 2
    At the top of your php script, try adding: `` and then reloading the page. Then report back on any errors if any that are displayed. – JDSchenck Feb 13 '18 at 06:51

1 Answers1

1

You can not use raw PHP functions etc in HTML files, try to change the extension to .php maybe this helps you.

REJack
  • 29
  • 1