Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 626 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Possible to convert result of Drupal db_query to PHP array?

#1
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?
Reply

#2
Not in Drupal 6.

In Drupal 7, there are fetch methods that can help to avoid loops like that. From

[To see links please register here]

:

<?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);
?>
Reply

#3
In Drupal 7, you can also use:

db_query('QUERY')->fetchAll(PDO::FETCH_ASSOC);
Reply

#4
One can also use [`db_fetch_array($result)`][1], where `$result = `[`db_query($queryString)`][2]. As explained from the Drupal documentation:

> [It returns] ...an associative array representing the next row of the result, or
> FALSE. The keys of this object are the names of the table fields
> selected by the query, and the values are the field values for this
> result row.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
I do always something like this ( just a simple exemple) :


$query = db_query("SELECT nid
FROM {from}
WHERE blallala
",
$tab_arg
);
if ($query->rowCount() == 0) {
$output=t('no result')
} else
{
foreach($query as $result)
{
$tab_res[]=$result;
}
foreach($tab_res as $res)
{
$output.=$res->nid;
}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through