\db_fetch_array
function
Fetch one result row from the previous query as an array.

Fetch one result row from the previous query as an array.

Comments

johnennew’s picture

The PHP default function return is null. If you pass something in which causes the if statement to skip then it will return NULL and not FALSE.

Did this recently and ended up with an endless loop ....

$result = db_query(''SQL with a typo");

while (($next_record = db_fetch_array($result) !== FALSE) {
some code
}

tmsimont’s picture

johnnydarkko’s picture

Just providing an example...
Needed to get a list of organic groups group node id's that a user is a member of.

<?php
function MYMODULE_groups_by_user($account = NULL) {
  if (!$account) {
    global $user;
    $account = $user;
  }
  $uid = $account->uid;
  $sql = "SELECT node.nid AS nid,";
  $sql .= " node.title AS node_title";
  $sql .= " FROM node node";
  $sql .= " LEFT JOIN og_uid og_uid ON node.nid = og_uid.nid";
  $sql .= " WHERE (node.status = 1) AND (node.type IN ('group'))";
  $sql .= " AND (og_uid.uid = %d)";
  $query = db_query($sql, $uid);
  while ($records = db_fetch_array($query)) {
    $nids[] = $records['nid'];
  }
  return $nids;
}
?>

HTH!