1

im trying to create my first website and Im clueless in this case. So I have a MySQL-Database with a table. And I have a php-File called database.php which reads from the database and echos all the lines of a query:

<?php
$servername = "xxxxxxxxxx.hosting-data.io";
$username = "xxxxxxxx";
$password = "xxxxxxx";
$dbname = "xxxxxxx";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT ID, Name, Beschreibung, Datum, Uhrzeit FROM Termine";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "ID: " . $row["ID"]. " - Name: " . $row["Name"]. " - Beschreibung: " . $row["Beschreibung"].  " - Datum: " . $row["Datum"]. " - Uhrzeit: " . $row["Uhrzeit"]."<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>

Now on my index.php I want to execute this php-code on calling/loading the webpage and print all the lines (data entries). But i have no idea how to get the echo (=data entries) of the php file printed in the body of my webpage. I read about AJAX and using a js-script but I still wasnt able to figure it out.

Thanks for your help.

1
  • So you want to echo your MySQL rows into the body of an HTML document? In this case you have at least two options: 1. move your PHP code into the body tag of your HTML (a .php file can contain both HTML and PHP) or 2. call your PHP file via AJAX and load the returned content into the body tag dynamically. Commented Apr 14, 2019 at 21:32

2 Answers 2

4

Option 1: Place your PHP code inside the HTML body.

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
  <?php
    echo 'Hello World'; 
    // ...
  ?>
</body>
</html>

Option 2: Create a separate PHP file containing your code above and include/require it into your body.

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
  <?php
    include_once('your_php_file.php');
  ?>
</body>
</html>

Option 3: Call your PHP file using an AJAX call (e.g. by using jQuery load()).

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
  <div id="aDiv"></div>
  <script> $( "#aDiv" ).load('your_php_file.php', function() { console.log('Loaded'); });</script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

@Samy Dressel please mark your accepted answer as accepted in order to help other people to identify the working solution faster. In case of questions feel free to reach out to us.
0

If your index file is index.php then the PHP code will be run when you load the webpage. That is assuming, of course, that your web server (local or remote) has PHP installed.

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.