gpt4 book ai didi

php - 从数据库中获取数据并在 php 中使用循环

转载 作者:行者123 更新时间:2023-11-29 04:07:00 25 4
gpt4 key购买 nike

我正在做大学项目,但我现在被困住了。

我有一个表,它有多个记录。记录由用户保存。我想在网页上显示特定用户存储的所有数据(在单独的 div 下)。我可以使用以下代码从数据库中获取所有数据。

$prj= mysql_query("select * from project where uid=$uid");
$record= mysql_fetch_assoc($prj);
$project_name = "$record['project_name']";
$short_dis= "$record['short_dis']";
$poster= "$record['poster']";

mysql_close($prj);

这是一些 php 代码。

    <div class="media services-wrap55 wow fadeInDown">
<img class="img-responsive" src="uploads/poster/photo-original_827156899.jpg"><br>
<a href="#"> <h4 class="media-heading">second project for testing</h4></a>
<p> by user</p>
<p> second project for testing second project for testing second project for testing second project for testing.</p>

<a class="" href="iska-link.html">More ... </a>
<p>
<div id="progressBar2" class="tiny-green"><div></div></div><p>
<div class="counter-inner"><div id=example1" data-countdown="07/02/2015 00:11:00"></div></div><p>
<div class="col-sm-11 text-right">
<div class="entry-meta">

<span><i class="fa fa-comment"></i><a href="comments"> 2 Comments </a></span>
<span><i class="fa fa-thumbs-up"></i><a href="#"> 56 </a></span>
<span><i class="fa fa-thumbs-down"></i><a href="#"> 56 </a></span>
<span><i class="fa fa-star"></i><a href="#"> 56 Fav </a></span>
</div>
</div>
</div>
</div>

如您所见,这些是静态数据。我想将数据库中的动态数据放入其中。

有没有建议如何通过循环获取div中的数据。

我可以使用 echo 显示一条记录。我不是在寻找完整的代码,只是想要数据库部分和一些示例,同时在 php 中使用它。

最佳答案

我不知道你的表结构,所以给出了一个示例代码,这样你就可以利用它并做相应的事情:-

    <?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$conn = mysqli_connect('hostname','username','password','databasename');

$prj= mysqli_query($conn,"select * from project where uid=$uid") or die(mysqli_error($conn));
$record = array();
while($row = mysql_fetch_assoc($prj)){
$record[] = $row;
}
mysql_close($conn);
?>
<?php foreach($record as $rec){?>
<div class="media services-wrap55 wow fadeInDown">
<img class="img-responsive" src="uploads/poster/<?php echo $rec['image_path'];?>"><br> // assuming that image_path is the field in table where you putted image name
<a href="#"> <h4 class="media-heading"><?php echo $rec['project_name'];?></h4></a>// assuming that project_name is the field in table where you putted project name
<p><?php echo $rec['user_name'];?></p>// assuming that user_name is the field in table where you putted user name
<p><?php echo $rec['project_description'];?></p>// assuming that project_description is the field in table where you putted project name
<!-- in the same way you can do for others also -->
<a class="" href="iska-link.html">More ... </a>
<p>
<div id="progressBar2" class="tiny-green"><div></div></div><p>
<div class="counter-inner"><div id=example1" data-countdown="07/02/2015 00:11:00"></div></div><p>
<div class="col-sm-11 text-right">
<div class="entry-meta">

<span><i class="fa fa-comment"></i><a href="comments"> 2 Comments </a></span>
<span><i class="fa fa-thumbs-up"></i><a href="#"> 56 </a></span>
<span><i class="fa fa-thumbs-down"></i><a href="#"> 56 </a></span>
<span><i class="fa fa-star"></i><a href="#"> 56 Fav </a></span>
</div>
</div>
</div>
</div>
<?php } ?>

注意:- 停止使用 mysql_*,它已被正式弃用。使用 mysqli_*PDO

关于php - 从数据库中获取数据并在 php 中使用循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31057877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com