Get data from database and use loop in php

时间:2015-06-25 18:20:22

标签: php mysql

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.

3 个答案:

答案 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)

// database query to get result $result = mysqli_query($conn, "select * from project where uid=$uid") // new array for all rows $rows = array(); // run mysql_fetch_assoc() in a loop (iterate) to get all rows from // the whole result set and reassign them to the prepared and // empty $rows array // so that you can later iterate rows again to output your divs while($row = mysql_fetch_assoc($result)){ $rows[] = $row; } mysql_close($conn); // now iterate the $rows array to output data for each $row <?php foreach($rows as $row){ ?> // debug output to see what data is in a row var_dump($row); // print value of a key from the row array echo $row['some_keyname']; // use stringconcatenation to build your html output, e.g. div echo '<div>' . $row['some_keyname'] . '</div>'; <?php } ?>