php - 从数据库中选择并在 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>
解决方案
您正在从数据库中选择所有内容。因此,您必须使用循环来显示您从数据库中选择的内容。写类似这样的东西:
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); ?>
希望这会有所帮助。
推荐阅读
- javascript - 警告:道具类型失败:类型为“字符串”的无效道具“错误”预期为“布尔”
- reactjs - react-draft-wysiwyg - 警告:不能调用 setState
- microsoft-graph-toolkit - mgt-person : 打开用户聊天窗口
- java - 不兼容的类型 Void 和 Object - Java 泛型
- python - 创建子类不重复匹配字段
- wordpress - 警告:“继续”定位开关相当于“中断”
- r - 如何在 R 中使用 lapply 改变多个参数
- jdbc - 多个 JDBC 连接器 一个主题
- java - 存储临时文件 Java
- laravel - 从 Laravel Nova 下载存储在 AWS S3 中的文件会导致错误