2

I code my php code in another .php file and i need it to be included in the html file to retrieve the information from my database and paste it into a table.

So in other words i want my .php to be included so it will show the info

I tried using <?php include 'database.php';?> But it doesnt seem to work

HTML:

<body>
<button class="dbknapp" onclick="window.location.href = 'index.html';">Klikk     her for å legge inn informasjon!</button><br><br>
<table>
    <tr>
    <th>Id</th>
    <th>Fornavn</th>
    <th>Etternavn</th>
    <th>Email</th>
</tr>
</table>

<?php include 'database.php';?>
</body>

PHP:

<?php
$servername = "localhost";
$database = "db";
$username = "root";
$password = "philip123";

// Create connection

$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, name, lastname, email FROM Students"
$result = $conn-> query($sql);

if ($result-> num_rows > 0) {
    while ($row = $result-> fetch_assoc()) {
        echo "<tr><td>". $row["id"] ."</td><td>". $row["name"] ."</td><td>".             $row["lastname"] ."</td><td>". $row["email"] ."</td></tr>";
}
echo "</table>";
}
else {
echo "0 results";
}

$conn-> close();


?>
Philip
  • 31
  • 4
  • "it doesnt seem to work" -> what do you mean? Is there any error? –  Feb 28 '19 at 23:58
  • Im not getting any error but the data wont get retrieved from the database so im worried the html file isnt including the php script to run it. – Philip Feb 28 '19 at 23:59

3 Answers3

1

The default way is to have PHP files with the .php or .phtml extension. Files with the extension .html are not parsed by the server by default. You should rename your HTML files containing PHP tags into .php files.

If you really do want files with the .html extension to be parsed by the PHP module, you can reconfigure your server adding the PHP handler to files ending with .html. This can be done in the server configuration, virtual-host configuration or even .htaccess per directory (if AllowOverride is set in the configuration accordingly).

mod_php handler:

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

This enables the PHP-Module for each file having at least one arbitrary character before the literal dot of the extension .html, i.e. excluding files with no name except the extension only. The pattern is a regular expression. You can build expressions like <FilesMatch "^(file1|file2)\.html$"> as well in order to handle specific files only as parsable PHP files.

There are different configurations for cgi, fcgi and fpm PHP integration. The code above is the mod-php setting.

Quasimodo's clone
  • 5,511
  • 2
  • 18
  • 36
0

You cannot include (embed) PHP code in the HTML document. Change the document extension from .html to .php and everything will work fine. Or since you have so little code you can put it in just one file.

Try this:

index.php file will contain:

    <!DOCTYPE html>
    <html>
    <head>
       <meta charset="utf-8" />
       <title>Simple PHP Script</title>
    </head>
    <body>
    <button class="dbknapp" onclick="window.location.href = 'index.php';">Klikk     her for å legge inn informasjon!</button><br><br>

    <table>
        <tr>
        <th>Id</th>
        <th>Fornavn</th>
        <th>Etternavn</th>
        <th>Email</th>
    </tr>
    <?php include 'database.php'; ?>
    </table>

    </body>
    </html>

database.php file will contain:

<?php
$servername = "localhost";
$database = "db";
$username = "root";
$password = "philip123";

// Create connection

$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, name, lastname, email FROM Students"
$result = $conn-> query($sql);

if ($result-> num_rows > 0) {
    while ($row = $result-> fetch_assoc()) {
        echo "<tr><td>". $row["id"] ."</td><td>". $row["name"] ."</td>".$row["lastname"] ."</td><td>". $row["email"] ."</td></tr>";
}
echo "</table>";
}
else {
echo "0 results";
}

$conn-> close();
?>
Ylber Veliu
  • 351
  • 1
  • 12
  • I cant have php code and html code in the same file or it will show parts of the php code on the website and it wont run the php script. I have had this problem for the past days with multiple different scripts – Philip Mar 01 '19 at 00:07
  • You cannot have PHP code on HTML document! You can try for all year and it will not work. It just doesn't work that way. Change the .html to .php and everything will work fine. – Ylber Veliu Mar 01 '19 at 00:10
  • So i'll copy paste my html code onto my php file and save it as index.php? – Philip Mar 01 '19 at 00:18
  • Check the full answer above! – Ylber Veliu Mar 01 '19 at 00:30
0

You cannot execute PHP code in .html pages (unless some .htaccess config is done). The easiest way is to rename your .html file to .php.

Then, you can use both HTML and PHP in the same page. You can also include other PHP scripts in this .php page.

<!DOCTYPE html>
    <html>
    <head>
       <meta charset="utf-8" />
       <title>Simple PHP Script</title>
    </head>
    <body>
    <button class="dbknapp" onclick="window.location.href = 'index.php';">Klikk     her for å legge inn informasjon!</button><br><br>
       <table>
        <tr>
           <th>Id</th>
           <th>Fornavn</th>
           <th>Etternavn</th>
           <th>Email</th>
       </tr>
       <?php include 'database.php'; ?>
       </table>
    </body>
</html>

Then, database.php (I have formatted your code to make it readable)

<?php
$servername = "localhost";
$database = "db";
$username = "root";
$password = "philip123";

// Create connection

$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
   die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, name, lastname, email FROM Students";
$result = $conn-> query($sql);

if ($result-> num_rows > 0) {

    while ($row = $result -> fetch_assoc()) : ?>

        <tr>
            <td><?= $row['id'] ?></td>
            <td><?= $row['name'] ?></td>
            <td><?= $row['lastname'] ?></td>
            <td><?= $row['email'] ?></td>
        </tr>

    <?php endwhile;

} else {
    echo "0 results";
}
$conn-> close();

If you need to execute .html as .php see this post.