首页 > 解决方案 > 从数据库中选择并在 html 页面上回显

问题描述

我试图回显插入到我的数据库中的信息。由于我对 php 编码很陌生,我不太确定我是如何做到这一点的。我的数据库名称是“nyheter”,表是“post”。所以我想从 db 中选择数据并在页面上回显。

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "nyheter");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
//get results from database
$result = mysqli_query($connection,"SELECT * FROM post");
$all_property = array();  //declare an array for saving property
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<link rel="stylesheet" href="nyheter.css" type="text/css" />
 <link rel="stylesheet" href="read.css">
     <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>   
    <script src="jquery.dynamicmaxheight.js"></script>
</head>
<body>
<div class="allt-2">
<div class="content">
    <img src="http://cdn4.iconfinder.com/data/icons/socialmediaicons_v120/48/google.png"/ alt="" >
    <h3><?php echo $Name?></h3>
</div>​
<section class="section js-dynamic-height" data-maxheight="150" >   
<p class="dynamic-height-wrap"> Hej
</p>
 <button class="js-dynamic-show-hide button" title="Läs mer" data-replace-text="Läs mindre">Läs mer</button>
</section>
<img class="ny-img" src="http://placehold.it/500x320"/>
</div>
</body>
<script>
  $(document).ready(function(){
            $('.js-dynamic-height').dynamicMaxHeight();
        });
		</script>
</body>
</html>

标签: phphtml

解决方案


您正在从数据库中选择所有内容。因此,您必须使用循环来显示您从数据库中选择的内容。写类似这样的东西:

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
    echo "name: " . $row["name"]."<br>";
}
} else {
   echo "0 results";
}
mysqli_close($conn); ?>

希望这会有所帮助。


推荐阅读