【问题标题】:Select from database and echo on html page从数据库中选择并在 html 页面上回显
【发布时间】:2019-02-21 04:01:11
【问题描述】:

我正在尝试回显插入到我的数据库中的信息。由于我对 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>

【问题讨论】:

  • 究竟是什么不工作?你有错误吗?如果是这样,它们是什么?你什么都没看到吗?我们需要更多信息。
  • 不,我没有收到任何错误,它只是不会将数据回显到

    标题。

  • 你在哪里定义$name
  • 在您的代码中尝试编写类似这样的内容,其中您回显名称: if (mysqli_num_rows($result) > 0) { //在此处执行您的代码或在此处回显您的名称 }else{ //显示这里有错误; }
  • 在 addpost.php.

标签: php html


【解决方案1】:

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

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); ?>

希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 2014-08-30
    • 2013-10-29
    • 2014-10-06
    • 1970-01-01
    相关资源
    最近更新 更多