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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:47:45+00:00 2026-05-23T04:47:45+00:00

In Drupal, I can execute a SQL as follows: $query_object = db_query(SELECT * FROM

  • 0

In Drupal, I can execute a SQL as follows:

$query_object = db_query("SELECT * FROM {nodes}");

If I know the query returns only a single result (so only 1 row and 1 column), I can directly fetch it with:

$result = db_result($query_object);

If I got multiple results, I need to loop through them with something like:

$rows[] = array();
while (($row = db_fetch_object($query_object) != FALSE) {
  $rows[] = $row;
}

I’m wondering if there is an easier way to do that? Is there a way that I can transfer all results into an array with a single statement? Or isn’t that working, because db_result returns a cursor-like object, where you can only fetch a single row each time?

  • 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-23T04:47:46+00:00Added an answer on May 23, 2026 at 4:47 am

    Not in Drupal 6.

    In Drupal 7, there are fetch methods that can help to avoid loops like that. From http://drupal.org/node/310072:

    <?php
    // Retrieve all records into an indexed array of stdClass objects.
    $result->fetchAll();
    
    // Retrieve all records into an associative array keyed by the field in the result specified.
    $result->fetchAllAssoc($field);
    
    // Retrieve a 2-column result set as an associative array of field 1 => field 2.
    $result->fetchAllKeyed();
    // You can also specify which two fields to use by specifying the column numbers for each field
    $result->fetchAllKeyed(0,2); // would be field 0 => field 2
    $result->fetchAllKeyed(1,0); // would be field 1 => field 0
    
    // Retrieve a 1-column result set as one single array.
    $result->fetchCol();
    // Column number can be specified otherwise defaults to first column
    $result->fetchCol($column_index);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1) Can I automatically receive an email from my Drupal website when new updates
In my drupal installation I can get the offset from UTC in seconds to
How can I in Drupal define that menu item link is available only for
How can I make Drupal generate absolute urls for links and img paths from
What order does Drupal execute it's _cron hooks? It is important for a certain
I would like to execute the jQuery $(document).ready() in a drupal site. While i
We designed a search engine but it takes much time to execute its query
Can I install Drupal on a server with 256MB ram ? If yes, how
How can I implement HTTP authentication for a Drupal based web app in order
This question os partially a Drupal question and partially a SQL question. I have

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.