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

  • Home
  • SEARCH
  • 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

Drupal Aggregator core module is a useful one but suffers many problems. There are
Drupal is very much a Do Everything CMS. There are modules that allow you
My drupal site (internal) will not display the TinyMCE editor when using Google Chrome
I have Drupal 5 site where a button is clicked and it calls a
I'm running Drupal 5 on my website and want to upgrade to V6. I've
I'm a Drupal newbie. Is it possible to set up everything and deploy Drupal
Setup is following: Drupal project, one svn repo with trunk/qa/production-ready branches, vhosts for every
I have a Drupal module page where I am populating a form with a
If all CMS's (Drupal, MediaWiki) are just a collection of PHP or ASP in
In a custom module for drupal 4.7 I hacked together a node object and

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.