Display Mysql data in a table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

Display Mysql data in a table

Post by jackie111 »

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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

$query = "select * from myTable where blah = 'blah'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
  echo $row['1'];
  //etc
}
notice the quotes around the 1 in the $row[] array. I put those there because of the sample you gave of your table structure. What that does is actually use the key value of '1' which could be anywhere in the array depending on where it is in the table. if you use $row[1] however, it's going to echo the second value in the array (arrays are 0 based).
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

Post by jackie111 »

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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

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 :lol:

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>
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

Post by jackie111 »

but i only need 3 cols in the table.
hope this make sense.
Jackie
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

jackie111 wrote:but i only need 3 cols in the table.
hope this make sense.
Jackie
So change the query to only pull out the columns you need instead of pulling out *.

Are you planning on learning PHP/MySQL or just getting somebody to do it for you? :roll:

http://www.mysql.com/
http://www.php.net/
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post by Trenchant »

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>";
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

Post by jackie111 »

WOW, thanks, it works just fun! thanks sooooo omuch!! :)
Post Reply