0

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 ?

2

Here you can replace $sql with $result.

<?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 0 => field 1.
$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
// 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.
$result->fetchCol();
// Column number can be specified otherwise defaults to first column
$result->fetchCol($column_index);
?>
  • Hi, yea I've tested fetchAll() but looks like something wrong – Mitch Jul 13 '15 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 Jul 13 '15 at 8:45
  • So now i can make if ('n.uid' != 1) ect ? – Mitch Jul 13 '15 at 8:52
  • 1
    Yes sure when there is everything is available then its easy to put conditions. – user49660 Jul 13 '15 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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