Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4616726
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:01:28+00:00 2026-05-22T02:01:28+00:00

I have a mysql result (having many rows) that I will use later. For

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:01:28+00:00Added an answer on May 22, 2026 at 2:01 am

    What do you use for working with DB? PDO? MySQLi? MySQL extension?

    If you use MySQL extension (mysql_connect(), mysql_query() etc), you can use mysql_data_seek() to move the internal row pointer of the MySQL result:

    $res = mysql_query($sql);
    if ( mysql_num_rows($res) ) {
        // process first 5 lines
        for ( $n = 0; $n < 5; ++$n ) {
            $row = mysql_fetch_assoc($res);
            if ( $row === false ) {
                break;
            }
            // do something with $row
            // ...
        }
        // reset pointer
        mysql_data_seek($res, 0);
        // process all rows
        while ( $row = mysql_fetch_assoc($res) ) {
            // do something with $row
            // ...
        }
    }
    mysql_free_result($res);
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL query that returns a result with a single column of
I have a MYSQL stored procedure SP1() that returns a result set. I want
I have a php script that runs a mysql query, then loops the result,
I'm having the following problem with 2 MySQL tables that have a relation: I
I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass
I have a method that basically returns the results of a mysql query. public
I have a MySQL table called items that contains thousands of records. Each record
I'm having a bit of a problem with converting the result of a MySQL
I have a table t_s_list having s_id as primary key. Now I want to
I have a MYSQL database that contains data on my companies customers as they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.