Reset query resource result
Posted: Tue Mar 11, 2003 10:38 am
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
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