This is a simple query

$sql = db_select('node','n');
$sql->fields('n', array('uid','title'));
$s = $sql->execute();

I need to check the uid of all records :

if('n.uid' !=1 ) { do some stuff}

Should I store query result into an array before the if condition ? If yes how to do it ?

1 Answer 1


Here you can replace $sql with $result.

// Retrieve all records into an indexed array of stdClass objects.

// Retrieve all records into an associative array keyed by the field in the result specified.

// Retrieve a 2-column result set as an associative array of field 0 => field 1.
// 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
// If you need an array where keys and values contain the same field (e.g. for creating a 'checkboxes' form element), the following is a perfectly valid method:
$result->fetchAllKeyed(0,0); // would be field 0 => field 0, e.g. [article] => [article]

// Retrieve a 1-column result set as one single array.
// Column number can be specified otherwise defaults to first column
  • Hi, yea I've tested fetchAll() but looks like something wrong
    – Mitch
    Commented Jul 13, 2015 at 8:42
  • 1
    Try this $result = db_query("SELECT nid, title FROM {node}"); and then use this $record = $result->fetchAll(); and var_dump($record); It will give your all results in array.
    – user49660
    Commented Jul 13, 2015 at 8:45
  • So now i can make if ('n.uid' != 1) ect ?
    – Mitch
    Commented Jul 13, 2015 at 8:52
  • 1
    Yes sure when there is everything is available then its easy to put conditions.
    – user49660
    Commented Jul 13, 2015 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.