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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I use mysql_fetch_object twice on one page? 1

Status
Not open for further replies.

KevinFSI

Programmer
Nov 17, 2000
582
US
Sorry, I'm an experienced programmer, but a novice when it comes to PHP. I think my question is very simple though. Lets say I have a query like this:

$result = mysql_query( "SELECT * FROM myTable );

Can I use that more than once on the same page? I tried outputting it into two tables, one right after the other, but only the first table is displayed.
Code:
echo "There are $catCount categories"
		."<BR><HR><BR>"
		."<TABLE ID=\"catTbl\" STYLE=\"border:1px black solid\" CELLSPACING=\"0\" CELLPADDING=\"5\">";
		
while ( $cat = mysql_fetch_object( $result ) )
{
	echo "<TR>"
				."<TD>".$cat->category."</TD>"
			."</TR>";
}
echo "</TABLE>";

echo "<BR><HR><BR>"
		."<TABLE ID=\"catTbl\" STYLE=\"border:1px black solid\" CELLSPACING=\"0\" CELLPADDING=\"5\">";

while ( $cat2 = mysql_fetch_object( $result ) )
{
	echo "<TR>"
				."<TD>".$cat2->category."</TD>"
			."</TR>";
}
echo "</TABLE>";

The second table doesn't display. Do I have to execute the query over and over again for every time I want to output the results?

Kevin
slanek@ssd.fsi.com

&quot;Life is what happens to you while you're busy making other plans.&quot;
- John Lennon
 

I think you need to use msql_data_seek and "rewind" the row to the start again. Since it returns a boolean (success/failure) I wrapped it in an if statement and built on your example.

Code:
[COLOR=red]if (msql_data_seek($result,0)) {[/color]
    while ( $cat2 = mysql_fetch_object( $result ) )
    {
        echo "<TR>"
            ."<TD>".$cat2->category."</TD>"
            ."</TR>";
    }
[COLOR=red]}[/color]

There may be better ways to do what you want rather than resetting the row and parsing the recordset again -- but then your example may very well have been just that :)

Cheers,
Jeff
 
I would build the script so the HTML produced can be handled within the same loop. Concatenate the second HTML table data into an buffer string var. Looping twice seems inefficient.
Print the buffered output into the correct spot.
 
Here's one way of doing it:
Code:
$tmp = array();
$tmp[] = 'There are '. $catCount . ' categories<BR><HR><BR><TABLE ID="catTbl" STYLE="border:1px black solid" CELLSPACING="0" CELLPADDING="5">';
while ( $cat = mysql_fetch_object( $result ) )
   $tmp[] = '<tr><td>' . $cat->category . '</td></tr>';
$tmp[] = '</TABLE>';
echo implode("\n",$tmp)."\n";
echo implode("\n",$tmp)."\n";

Since both of your tables are the same, I've generated the code once, stored it in a temporary array, and output it where it is needed.

Ken
 
The array was what I ended up doing. The example I gave wasn't really my desired output, it was just a way for me to ask the question. Thanks to all who answered.

Kevin
slanek@ssd.fsi.com

&quot;Life is what happens to you while you're busy making other plans.&quot;
- John Lennon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top