Can the content of a query be fetched multiple times?

Hi,

Can the content of a query be fetched multiple times?

I am pretty sure the answer to this is, or should be, yes, but a program that I have when I go to do this the 2nd time:

while ($result_get_kws = mysql_fetch_array($query_get_kws)) {

then nothing happens!
So What the HEK is going on :slight_smile:

FYI, the 2nd time I am checking for a different flag and generating the Table with different value arrangements based on the flag.

Regards,

I think mysql_data_seek will help you.

Hi,

I checked this page, but do not see the answer to my question!

So do you know that if it is possible, or not, to fetch data from a MySQL query multiple time? I see NO reason why this would not be yes, but again my attempts to fetch data from a query beyond the 1st fetch are not working.

Regards,

Isn’t it something like this you are looking for?


while($row = mysql_fetch_array($query_get_kws)){
    // print
}
echo '----------------------<br />';
mysql_data_seek($query_get_kws, 0);
while($row = mysql_fetch_array($query_get_kws)){
    // print
}

Sorry if I misunderstood :slight_smile:

Hi,

I am already doing that, via this method:

while ($result_get_kws = mysql_fetch_array($query_get_kws)) {

//get this data and do X1 with it

}

But when I go to use it again, that is:

while ($result_get_kws = mysql_fetch_array($query_get_kws)) {

//get this data and do X2 with it.

}

Then the data is not available!!!
So would I have to generate the Sql query again? I dont see why this would be the case since that would be very inefficient.

Regards,

You’ve missed Rajug’s call to mysql_data_seek in between the while loops. :wink:

What is preventing you to store the entire result of the query within an array and then use it as many times as you want in your script?
You can loop trough it as many times as you want, without repeating the query.

Hi,

Yes, I could do that.

But I wanted to know whether in MySQL/PHP the content of a query remain available to be fetched again or they are available for fetching only once? If only once, then this sounds really stupid, really inefficient. So I am just wondering about this question and not that I dont have other solutions for getting the data that was retrieved by the Query command.

If there is a solid solution provided for that particular reason then you cannot say that it is stupid. The query result does not loose its result but when looping, the internal result pointer goes at the last and you need to set it to the first (0) before you use it. What is the problem using mysql_data_seek() function?

Aha, now I see what is going on.
So the data of query is not lost, but once one loops through the query then the counter remains on the last position and needs to be set back to the first(0) position. Got it. So does mysql_data_seek() do this?

I mean if my query is:

$query_get_kws = mysql_query($sql_get_kws) or die(mysql_error());

// and then this

while ($result_get_kws = mysql_fetch_array($query_get_kws)) {

//do x here

}

Then how do I reset the counter for $query_get_kws back to first(0)?

Is it:
mysql_data_seek($query_get_kws);

Regards,

Actually, I got my answer it is:

mysql_data_seek($query_get_kws, 0);

So I was forgetting to add the 0 into it and the , :slight_smile:

sigh :rolleyes: