Borislav Razvanski
Borislav Razvanski

Reputation: 75

PHP, HTML and MySqli - how to show all data from specific column in one table row

I have a database table called names. I have 2 columns there - "id" and "name". In the column "name" I have some data like:

Inna

Petia

Vaska

Kote

Pepa

I want this data to be shown in a html table like this:

<table>
  <tr>
    <td>Inna</td>
    <td>Petia</td>
    <td>Vaska</td>
    <td>Kote</td>
    <td>Pepa</td>
  <tr>    
</table>  

My PHP code is:

<?php
$q= mysqli_query($db, 'SELECT * FROM names');

echo '<table>';

while ($row = mysqli_fetch_assoc($q)){
echo '<tr>';
foreach($row as $value) {   
        echo "<td>$value</td> "; 
    }  
echo ' </tr>';
}

but this did not work for me!

Upvotes: 0

Views: 231

Answers (2)

Borislav Razvanski
Borislav Razvanski

Reputation: 75

<?php
$q= mysqli_query($db, 'SELECT * FROM names');
echo '<table>';
echo '<tr>';
while ($row = mysqli_fetch_assoc($q)){
foreach($row as $value) {   
    echo "<td>$value</td> "; 
}  
}
echo ' </tr>';

This is the solution of my problem: I just has to put

echo '<tr>';
echo '</tr>';

outside the while!

Upvotes: 1

Rachel Geller
Rachel Geller

Reputation: 322

try this:

echo '<table>';

while ($row = mysqli_fetch_assoc($q)){
    echo '<tr>';
    //foreach($row as $value) {   
        echo "<td>" . $row['name'] . "</td>"; 
    //    }  
    echo '</tr>';
}

Upvotes: 1

Related Questions