I am working on college project and i am stuck at the moment.
I have a table and it have multiple records. Records are saved by user. I want to show all data stored by specific user on webpage (under a separate div). I am able to fetch all data from database by using below code.
$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);
Here are some php code.
<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>
As you can see these are static data. I want to put dynamic data from database in this.
Is there any advise how to get the data in div by loop.
I can show one record by using echo. I am not looking for entire code just wanted database part and some sample while using it in php.
答案 0 :(得分:3)
First off, you should be using PDO to query your database instead of mysql_query(). You code as it is now allows for SQL injection.
Here's how I would do it:
$db = new PDO('mysql:host=CHANGE_THIS_TO_YOUR_HOST_NAME;
dbname=CHANGE_THIS_TO_YOUR_DATABASE',
'CHANGE_THIS_TO_YOUR_USERNAME',
'CHANGE_THIS_TO_YOUR_PASSWORD');
$sql='select * from project where uid = :uid';
$query = $db->prepare($sql);
$query->bindValue(':uid', $_REQUEST['uid']);
$query->execute();
$projects = $query->fetchAll();
Then to display it in a loop, do this:
foreach ($projects as $project) {
echo '<div class="project">';
echo '<span class="project-name">'. $project['project_name'] .'</span>';
echo '<span class="project-dis">'. $project['short_dis'] .'</span>';
echo '<span class="project-poster">'. $project['poster'] .'</span>';
echo '</div>';
}
答案 1 :(得分:2)
I didn't know your table structure, so a sample code is given so that you can take help of it and do accordingly:-
<?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 } ?>
Note:- stop using mysql_*
, it is officially deprecated. use mysqli_*
or PDO
.
答案 2 :(得分:1)