Jump to content


displaying MySQL results in a table

  • Please log in to reply
1 reply to this topic

#1 spectsteve7

  • Members
  • PipPip
  • Member
  • 12 posts

Posted 18 March 2006 - 02:18 AM

Hi there,

I'm new here and I'm looking for a bit of help. I have a table on my page. It's made of 1 row and two columns. I'm looking to query 1 field from my database and display all records with in the database. I'm looking to make as much use of space as possible so I want to have it populate the data in the first left and right cells then create and populate in a new row. Hopefully someone could point me in the right direction.


#2 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 19 March 2006 - 10:51 AM

Here's a sample script. Define NUMCOLS as required.
define ("NUMCOLS", 2);

$res = mysql_query("SELECT columname1, columname2 FROM tablename");

$count = 0;
echo "<TABLE border=1>";
while (list($col1, $col2) = mysql_fetch_row($res)) {

    if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row

    echo "<TD>$col<br>$col2</TD>\n";

    if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
if ($count % NUMCOLS != 0) {
   # end row if not already ended
   while ($count++ % NUMCOLS) echo "<td>&nbsp;</td>";
   echo "</TR>\n";
echo "</TABLE>";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users