I have a mysql result (having many rows) that I will use later. For now, I only need to access (sum) the first 5 rows. The problem is that the rows were saved from the query using “AS”, so now they can be accessed by using $row['name'] . How can I select the first 5 rows without using the “name” for each of them?
Is there any way for doing like so:
PHP:
for($i=0;$i<5;++$i)
echo $row[$i];
?
EDIT
Sorry, my question was wrong.
Actually: How can I use the same $result 2 times without loosing the values by fetching the array?
What do you use for working with DB? PDO? MySQLi? MySQL extension?
If you use MySQL extension (
mysql_connect(),mysql_query()etc), you can usemysql_data_seek()to move the internal row pointer of the MySQL result:Another option would be to fetch all results into an array and then work with that array. I can’t think of any benefit of holding MySQL resource opened.