简体   繁体   English

如何显示SQL数据库中的数据?

[英]How can I display my data from SQL database?

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. 由于我只是作为学校任务开始这个项目的,因此我在混合PHP和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. 显示单词“ tot”,但其余部分为空。 How can while loop over the data and display it per row in div's? while怎样遍历数据并在div的每一行显示它?

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. if语句相同。

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
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM