1

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.

5
  • 1
    If you can, you should stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's really not hard. Commented Jun 25, 2015 at 18:28
  • 1
    Manual first, Community last. Go read: php.net/manual/de/mysqli-result.fetch-array.php and comments. Commented Jun 25, 2015 at 18:29
  • I will try to use mysqli/PDO. At the moment i don't have idea about them. As i am still learning. But i will definitely try. However, if you advice on this question then it would be nice. Thanks Commented Jun 25, 2015 at 18:30
  • @JensA.Koch I already tried but i am not able to show it on webpage. because i think i need to generate div at runtime as well. Commented Jun 25, 2015 at 18:32
  • 1
    @404 wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers Commented Jun 25, 2015 at 18:35

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. Appreciate it. Voted up. Will test and let you know.
sure but change variables and values according to your table structure. i think you understand what i want to say.
I understood. One thing i want to know is in your code you put all the table data in array($record[] = $row;). Do i need to follow the same or i need to define table fields?
if you want to show limited number of fields then define it like $record['user_name'] = $row['user_name']; and so on . if you want to show all then no need.
4

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 Comment

Thanks for the answer. Appreciate it. Voted up. Will test and let you know.
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 } ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.