Reset query resource result

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
escaperanger
Forum Newbie
Posts: 2
Joined: Tue Mar 11, 2003 10:38 am

Reset query resource result

Post by escaperanger »

My question is about the resource result returned from mysql_query(). Can it be reset to the beginning of the results that get returned?

I am exporting table data from a databse and want to include the column names in the first row when I write the data to a text file. When getting data from the db and kicking out to the screen I generally use something like this:

for($x=0; $x<$num_rows; $x++)
{
$row = mysql_fetch_array($query_result);
echo $row;
}

That loops through all the rows returned in the query very nicely. In the export scenario above, however, I need to get a row. Write the names of the table columns in the first line of the text file and then write the data stored in each row to the file.

If I use the first row returned to write out the column names of the table I cannot access the data stored in that row. And the loop moves on to the next row. My workaround has been to query the db. Write my column name. Then query the db a second time, getting a fresh resource result, and looping through the entire result to display the data. Not a big deal since this routine might get called once a month.

But can I reset the resouce result in order to avoid querying the db twice?

Thanks
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: Reset query resource result

Post by twigletmac »

escaperanger wrote:can I reset the resouce result in order to avoid querying the db twice?
Yes you can: mysql_data_seek()
Mac
escaperanger
Forum Newbie
Posts: 2
Joined: Tue Mar 11, 2003 10:38 am

Post by escaperanger »

Thanks for the info and the convenient link.
Post Reply