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 7697777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:05:22+00:00 2026-05-31T22:05:22+00:00

as the question states. I have implemented a function wherein it fetches multiple rows

  • 0

as the question states.

I have implemented a function wherein it fetches multiple rows from a database (*1) and then (*2) instantiate each row as an Object. After instantiation, the Objects are then stored into an array and then return the result to caller function and then iterates through the result array and displays each Object and add html formatting for each.

Here is the snippet of the code:

function find_all() {
    //(*1) Fetch 30 comments from DB
    $sql  = 'SELECT * FROM comments';
    $sql .= ' ORDER BY datetime DESC LIMIT 30';
    return find_by_sql($sql);
}

function find_by_sql($sql='') {
    global $database;
    $result_set = $database->query($sql);
    $object_array = array();

    while($row = $database->fetch_array($result_set)) {
        //(*2) Instantiate each row to a Comment object
        //     and then stores each comment to an object array
        $object_array[] = Comment::instantiate($row); 
    }
    return $object_array;
}


//(*3) Format and display each result.
$comments = find_all();
foreach ( $comments as $comment ) {
    // Not sure if syntax is correct.. anyhow..
    echo "<li>$comment->get_text()</li>";
}

However, I like the above approach since it’s cleaner, easier to read, maintainable, and more OOP. But the problem is it takes a longer time to display than just simply iterating through each result than display each result once it’s fetched, like so:

while ($row = mysql_fetch_array($sql)) {
    echo "<li>$row['text']</li>";
}

I know the reason behind why it is slow. What I want to know is there a better way to solve the problem above?

While caching might be a good idea, it won’t help because I need an updated list every time the list is fetched.

  • 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-31T22:05:24+00:00Added an answer on May 31, 2026 at 10:05 pm

    I think it can be a little faster if the script gets only the part you are interested in the result set, because fetch_array() returns 2 arrays with the same result set: associative, and numeric.

    By adding MYSQLI_ASSOC (if you use mysqli): mysqli_fetch_array($result, MYSQLI_ASSOC), or try with mysql_fetch_assoc(), the script receives only the associative array.
    You can test in pmpMyAdmin to see the diferences.

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

Sidebar

Related Questions

As the question states, I have two ethernet devices I need to use. A
I have a question: I have two MSSQL tables, items and states, that are
I have two questions regarding ceil() function.. The ceil() function is implemented in C.
Lets say that I have a table containing users . Each row in that
I have implemented some functionality in C# using the yield statement with the function
I have a form implemented from hook_form called simplequiz_form() I want to access its
As the question states - is there a good tool out there which lists
As the question states, i am a C#/Java programmer who is interested in (re)learning
I would (as the question states) like to make an asynchronous call, preferably using
As the question states, what date format is ds1248083197360 ? Is this a standard

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.