简体   繁体   中英

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!

try this:

echo '<table>';

while ($row = mysqli_fetch_assoc($q)){
    echo '<tr>';
    //foreach($row as $value) {   
        echo "<td>" . $row['name'] . "</td>"; 
    //    }  
    echo '</tr>';
}
<?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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM