Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display more than one row in a table 1

Status
Not open for further replies.

rskuse

Technical User
Jul 18, 2002
74
0
0
GB
Hi, I'm using the following code to query my database table and display the data on a page:

//connect to the DB
include('db.php');
//set up query
$query = "SELECT * FROM clients,projprog WHERE clients.clientid = projprog.clientid AND clients.clientid = 8";
//run query
$result = mysql_query($query, $connection) or die(mysql_error());

while($row = mysql_fetch_array($result)) {
$date = $row['date'];
$postby = $row['postby'];
$subject = $row['subject'];
$post = $row['post'];
}

?>

<html>
<body>
<?php do { ?>
<table>
<tr><td><?php echo $date; ?></td><td><?php echo $postby; ?></td></tr>
<tr><td><?php echo $subject; ?></td></tr>
<tr><td><?php echo $post; ?></td></tr>
</table>
<?php
} while ($row = mysql_fetch_array($result));
?>
</body></html>
<?php mysql_free_result($result);?>

Although this works fine it only displays one row from my table - how do I get it to display all the rows I'm asking for?

Would I be right in thinking i have to use something like:
$totalRows = mysql_num_rows($result);

Any help much appreciated...
 
It's probably only showing the last row of your result set.

Look at what your code is doing. You query the database and set some variables to the values returned. But you keep doing that in your first while loop. So each row of the result set is fetched in turn, and the variables are updated. So when that loop finishes, the variables have the values of the last row in your result set.

Then you have another loop to fetch more rows. But it only runs because you are using a do...while loop, which will always run once. That loop runs the requisite one time, outputs the values of the variables (set to the last values from the result set in the previous loop), then quits because mysql_fetch_array() will return false because all the rows of the result set have been fetched.

Here is a restructuring of your code that will probably work better:

Code:
<?php
//connect to the DB 
include('db.php');

print '<html><body><table>';

//set up query 
$query = &quot;SELECT * FROM clients,projprog WHERE clients.clientid = projprog.clientid AND clients.clientid = 8&quot;;
//run query 
$result = mysql_query($query, $connection) or die(mysql_error());
    
while($row = mysql_fetch_array($result))
{
	print '
	<tr>
		<td>' . $row['date'] . '</td>
		<td>' . $date['postby'] . '</td>
    	<td>' . $row['subject'] . '</td>
    	<td>' . $row['post'] . '</td>
    </tr>';
}

print ' </table></body></html>';
	
mysql_free_result($result);
?>

Want the best answers? Ask the best questions: TANSTAAFL!!
 
No, need simply place the html to be repeated into the while loop like this:
Code:
//connect to the DB 
                             include('db.php');
                             //set up query 
                             $query = &quot;SELECT * FROM clients,projprog WHERE clients.clientid = projprog.clientid AND clients.clientid = 8&quot;;
                             //run query 
                             $result = mysql_query($query, $connection) or die(mysql_error());
                             
                             //start the html
                             echo &quot;<html><body><table>&quot;;

                             while($row = mysql_fetch_array($result)) {
                                 $date = $row['date'];
                                 $postby = $row['postby'];
                                 $subject = $row['subject'];
                                 $post = $row['post'];
                            
                                 echo &quot;<tr><td><?php echo $date; ?></td><td>$postby</td></tr>
                                         <tr><td>$subject</td></tr>
                                         <tr><td> $post</td></tr>&quot;;

                             }//end while loop
                             

                             //clean up html
                             echo &quot;</table></body></html>&quot;;
                             mysql_free_result($result);
                             ?>


Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
yep, that works much better - thankyou.

For future reference would you advice using the print function over echo? is there any big difference?

Thanks...
 
Thanks Sleipnir and thankyou Bastien - for some reason i find 'echo' easier to follow!!
 
Whichever you use is up to you. So long as you use it in lieu of context switching.

I've found that as code gets more complex, pages and page of code like:

Code:
for ($counter = 1; $counter <=5; $counter++)
{
  print 'foo';
}

Is by far and away more readable than:

Code:
<?php
for ($counter = 1; $counter <=5; $counter++)
{?>
  foo
<?php
}?>

Want the best answers? Ask the best questions: TANSTAAFL!!
 
I'll certainly remember that as and when I do attempt more complex code but i'm just at the bottom of the ladder at the moment!!

thanks for all your help and advice....
 
sleipnir,

You've earned yourself another star.

Your answers in this thread were very clear and easy to follow.

I like it when people give good clear answers. And like this time, sometimes it's appropriate to include a bit of code to help some of us less learned.
[soapbox]

By the way; congratulations on becoming the tipmaster for the week.

tgus

____________________________
Families can be together forever...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top