Display Mysql data in a table
Moderator: General Moderators
Display Mysql data in a table
hello
i need to display some data from mysql in a table, the table has 4 cols, looks like:
1------2------3------4
5------6------7------8
.
.
.
how to do that? thanks again for the great help from this group!
jackie
i need to display some data from mysql in a table, the table has 4 cols, looks like:
1------2------3------4
5------6------7------8
.
.
.
how to do that? thanks again for the great help from this group!
jackie
Code: Select all
$query = "select * from myTable where blah = 'blah'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
echo $row['1'];
//etc
}i am sorry, but the code is wrong, i meant, there are serval records(rows) in the table, let's suppose there are 6 lines, such as
1-----tom----26----male
2-----tt-----34-----female
3-----yy-----25-----male
4-----uu-----12-----male
5-----ee-----45-----female
6-----cc-----34-----male
i need to display in a table like this:
---------------------
tom-----tt-----yy
uu-----ee-----cc
--------------------
Do you understand now? i hope someone could help me.
jackie
1-----tom----26----male
2-----tt-----34-----female
3-----yy-----25-----male
4-----uu-----12-----male
5-----ee-----45-----female
6-----cc-----34-----male
i need to display in a table like this:
---------------------
tom-----tt-----yy
uu-----ee-----cc
--------------------
Do you understand now? i hope someone could help me.
jackie
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Burrito's code was correct 
It may have been a bit more generic than the specific case you're using here but you should be able to change the query and the output to match your needs...
Ah sod it... I'll do but you'll have to kinda try modifying code to suit your needs
It may have been a bit more generic than the specific case you're using here but you should be able to change the query and the output to match your needs...
Ah sod it... I'll do but you'll have to kinda try modifying code to suit your needs
Code: Select all
<table>
<?php
$query = "SELECT * FROM `myTable` WHERE `gender` = 'female'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
foreach ($row as $v) {
echo '<td>'.$v.'</td>';
}
echo '</tr>';
}
?>
</table>- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
So change the query to only pull out the columns you need instead of pulling out *.jackie111 wrote:but i only need 3 cols in the table.
hope this make sense.
Jackie
Are you planning on learning PHP/MySQL or just getting somebody to do it for you?
http://www.mysql.com/
http://www.php.net/
Heres like a bit of a working tutorial. I would suggest you learn more PHP as it will help you in the long run.
Code: Select all
<?php
// First set the query to find the information in the database. *the map*
$sql = mysql_query("take all the infromation here") or DIE(MYSQL_ERROR());
// Now set $count to 0 (just to make sure everything works okay
$count = '0';
// So thats done. Now repeat through all rows of the database and output the information
echo "<table border=1><tr>";
while($sel = mysql_fetch_array($sql)) {
// Print out aall the information
echo "<td>".$sel[row name]."</td>";
// Now add 1 to count.
$count = $count + '1';
// This if statement decides whether or not to add a new <tr>
// Change the value to how many columns you want. ex. 3 or 4
if ($count == '3') {
echo "</tr>";
$count = '0';
}
}
// Now that the information is there finish off the table.
if ($count != '0') {
echo "</tr>";
}
echo "</table>";