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

Looping over a query result twice 1

Status
Not open for further replies.

Sarky78

Programmer
Joined
Oct 19, 2000
Messages
878
Location
GB
Hi,

First off appologies to people who have read this on other forums, as I have posted it in various places.

I have got an include file that contains my query to return the countries that are within my database. I have then written a function that loops over that result set and creates a HTML select.

This works fine the first time I use the function, but the second time it doesn't output anything!

I have a feeling that this is something to do with the where the record pointer is at!

Is there anyway of moving the pointer back to the start of the records?

this is the output code:

function writeSelect($FormFieldName, $dbObject, $JoinField, $UpdateIdent, $DisplayValue, $ItemMatch) {
print (&quot;<select name=\&quot;$FormFieldName\&quot;>\n&quot;);
while ($SelectRow = mysql_fetch_assoc($dbObject)) {
print (&quot;<option value=\&quot;$SelectRow[$JoinField]\&quot;&quot;);
if (IsSet($_GET[$UpdateIdent])) {
if ($SelectRow[$JoinField] == $ItemMatch) {
echo &quot; selected&quot;;
}
}
print (&quot;>$SelectRow[$DisplayValue]</option>\n&quot;);
}
print (&quot;</SELECT>&quot;);
}

TIA

Tony
 
that shouldnt be a problem... when u pass the object to the function only a copy is passed. therefore technically this must work:
$rs=mysql_query($sql);
writeSelect(...., $rs,....);
writeSelect(...., $rs,....);

Known is handfull, Unknown is worldfull
 
Yeah, thats what i thought!

But when i run the page, i get the second lot of select output to the screen, but there is nothing in them.

For thr countries I have got the one query, that I am brining in from a include file, this is called $objCountries

<? writeSelect(&quot;name_of_form_element&quot;, $objCountries, various other variabes)?>

I have also tried to loop over the query on a page, with the query defined at the top of the page, and two while loops, just outputting the counrynames to the page, and the first one works fine, but when i come to the second one nothing is output. Most werid!

I am using PHP 4.3.0 on a WinXP Pro system at the moment, but this hopefully gonna work on all platforms!

Any ideas?

Tony
 
for instance this code below, produces the following:

//start of output
Germany
none
UK
united kingdom
Wiltshire

Second iteration
//end of output

<?
require(&quot;includes/connect.php&quot;);

$strSQL = &quot;SELECT * FROM tblCountries ORDER BY CountryName&quot;;
$objCountries = mysql_query($strSQL);
$CountryCount = mysql_num_rows($objCountries);

function doOutput($objectLoop) {
while ($row = mysql_fetch_array($objectLoop)) {
echo stripslashes(trim($row[&quot;CountryName&quot;])) . &quot;<BR>&quot;;
}
}

echo &quot;first iteration<BR>&quot;;
doOutput($objCountries);
echo &quot;<BR>Second iteration<BR>&quot;;
doOutput($objCountries);
?>

Tony
 
i will check this out.....

Known is handfull, Unknown is worldfull
 
O.K i give up. it seems that the pointer has simply moved on or the mysql_fetch_assoc has simply deleted the values....

:(

Known is handfull, Unknown is worldfull
 
thats what I thought it might have done, I am glad that you have come to the same conclusion.

Dam it though, as it means I am going to have to use two connections to do the job of one!

Tony
 
Guys,

You have to take the nature of a mysql result identifier into consideration.

When you do a looped mysql_fetch_array() or something like it you move the internal pointer of the result identifier to the end. You can reset the pointer to the beginning of the result set by issuing the command:
Code:
mysql_data_seek($result,0);
Read:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top