Matthew Kelsay Matthew Kelsay - 1 year ago 59
MySQL Question

Seperate PHP Page, Loop through MySQL table, and Create New Divs

I am curious is it possible to create a loop in a separate PHP page that will create divs? Here is what I have been working on. I know that it will show the results for the first row in the data table. However, it does not show the following rows. I would assume since it does not show the following this won't work.

Separte PHP page

$query = "SELECT `ID`, `fullname`, `comment`, `date` FROM `reviews`";
$result = mysql_query($query);
die('Error: ' .mysql_error());
$row_count = mysql_num_rows($result);
$row_users = mysql_fetch_array($result);

for($i=0; $i<$row_count; $i++)
$results = '<div class="col-lg-4">Name:'.($row_users['fullname']).'<br>'.($row_users['comment']).'<br>Date: '.($row_users['date']).'</div>';


<div class="row">
<?php {echo $results;}?>

Answer Source

Create Div for Each MySQL Row

In PHP file that contains proper database information.

function getTable() {
    $query = "SELECT `ID`, `fullname`, `comment`, `date` FROM `reviews`";
    $result = mysql_query($query);

    $table = '';

    if ($result) {
        while($row_users = mysql_fetch_array($result)) {
            $table .= "<div class='row'>";
            $table .= '<div class="col-lg-4">Name:'.($row_users['fullname']).'<br>'.($row_users['comment']).'<br>Date: '.($row_users['date']).'</div>';
            $table .= "</div>";
    } else {
        die('Error: ' .mysql_error());

    return $table;

In HTML display file

<?php echo getTable(); ?>

PHP mysql Extension is deprecated!!!

The extension is deprecated as of PHP 5.5.0 and is removed in PHP 7.0.0.

Do not use the mysql class in PHP. It is not safe. Please use a library like mysqli or, my favorite, PDO

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download