0

I am building this website where people can add events to a calendar. I created a form that puts data into my database, but now I want to know the magic about how to get the data out of the database! Since I just started this project as a school assignment, I have a little struggle with mixing PHP and HTML. I hope I have done right;

<?php
include("template.php");

$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

?>

<body>
<div class="outer">
    <h2> Beheerderspagina </h2>
    <hr>

<?php
$sql = "SELECT name, organisator, begin, end, location, province, price, sort, website, description FROM events";
$result = $conn->query($sql);

if ($result->num_rows > 0); 
    while($row = $result->fetch_assoc());
        echo <<<HTML
            <div class="add">
                <div class="block">
                    <div class="photo">
                        <img src="geen_foto_beschikbaar.jpg" width="250px" height="250px">
                    </div>
                    <div class="text">
                        <div class="links">
                            <p> 
                            Naam:<br>Organisator:<br>Datum:<br>Locatie:<br>Provincie/land:<br>Gemiddelde prijs:<br>Genre:<br>Website:<br>Omschrijving </p>
                        </div>
                        <div class="rechts">        
HTML;
                            echo "<br>".$row["name"]."<br>".$row["organisator"]. "<br>". $row["begin"]. " tot ". $row["end"]. "<br>". $row["location"]. "<br>". $row["province"]. "<br>". $row["price"]. "<br>". $row["sort"]. "<br>". $row["website"]. "<br>". $row["description"]. "<br>";
    echo <<<HTML
                    </div>
                </div>
            </div>
        </div>
HTML;
?>

So this is the code, but it isn't working. The problem is, it isn't displaying my data. The word 'tot' is displayed, but the rest is just empty. How can while loop over the data and display it per row in div's?

1
  • It sounds like you might have a partial page rendering, stopped by an error. Try 'View source' to see if you can see any errors. Commented Jan 22, 2015 at 11:10

2 Answers 2

3
while($row = $result->fetch_assoc());

You don't need a ; here, wrap the code with {} instead. Like this:

   while($row = $result->fetch_assoc()){
        echo <<<HTML
            <div class="add">
                <div class="block">
                    <div class="photo">
                        <img src="geen_foto_beschikbaar.jpg" width="250px" height="250px">
                    </div>
                    <div class="text">
                        <div class="links">
                            <p> 
                            Naam:<br>Organisator:<br>Datum:<br>Locatie:<br>Provincie/land:<br>Gemiddelde prijs:<br>Genre:<br>Website:<br>Omschrijving </p>
                        </div>
                        <div class="rechts">        
HTML;
                            echo "<br>".$row["name"]."<br>".$row["organisator"]. "<br>". $row["begin"]. " tot ". $row["end"]. "<br>". $row["location"]. "<br>". $row["province"]. "<br>". $row["price"]. "<br>". $row["sort"]. "<br>". $row["website"]. "<br>". $row["description"]. "<br>";
    echo <<<HTML
                    </div>
                </div>
            </div>
        </div>
HTML;}

Same with if statement.

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

4 Comments

No, mysqli is not old and deprecated.The mysql_* family (notice the missing i) is deprecated.
Oh, okay, sorry for that.
But if I do so, the whole page doesn't display anymore ?
That means something gone wrong. Set error_reporting(E_ALL | E_STRICT);
1

Updated your loop to following

<?php
$sql = "SELECT name, organisator, begin, end, location, province, price, sort, website, description FROM events";
$result = $conn->query($sql);

if ($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
?>
            <div class="add">
                <div class="block">
                    <div class="photo">
                        <img src="geen_foto_beschikbaar.jpg" width="250px" height="250px">
                    </div>
                    <div class="text">
                        <div class="links">
                            <p> 
                            Naam:<br>Organisator:<br>Datum:<br>Locatie:<br>Provincie/land:<br>Gemiddelde prijs:<br>Genre:<br>Website:<br>Omschrijving </p>
                        </div>
                        <div class="rechts">        
                            <br><?=$row["name"]?>
                            <br><?=$row["organisator"]?>
                            <br><?=$row["begin"]?> tot <?=$row["end"]?>
                            <br><?=$row["location"]?>
                            <br><?=$row["province"]?>
                            <br><?=$row["price"]?>
                            <br><?=$row["sort"]?>
                            <br><?=$row["website"]?>
                            <br><?=$row["description"]?><br>
                    </div>
                </div>
            </div>
        </div>
<?php
    }
}
?>

2 Comments

thank you! your answer works the best for me so far. If I may ask, what does it mean when you run your page and it says; Trying to get property of non-object on line 38? in my case line 38 is the second line of the code you just gave me.
You still need to have the mysql connection statements on top of the script. $conn in $result = $conn->query($sql); refers to the connection variable

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.