0

This is the line:

$row = db_fetch_object($result);

While $result is valid but $row is getting a null value which I've checked with is_null.

As per this function's @return value, it can only return FALSE in failure.

Also mysql_db_object states the same.

What could be the reason it is returning null?

2
  • 1
    FALSE !== NULL ...
    – Jimajamma
    Dec 21, 2011 at 20:04
  • What is the actual query?
    – Malks
    Dec 21, 2011 at 21:45

1 Answer 1

0

Actually, db_fetch_object() could also return NULL, contrary to what the documentation says. The documentation assumes you aren't passing to the function a value that for PHP is equivalent to FALSE, for example NULL, an empty array, or an uninitialized variable. If the function gets a value that for PHP is equivalent to FALSE, the function will not execute any instruction, and it will return NULL.

function db_fetch_object($result) {
  if ($result) {
    return mysql_fetch_object($result);
  }
}

The same is true for other database engines, for example PostgreSQL, which uses the following function.

function db_fetch_object($result) {
  if ($result) {
    return pg_fetch_object($result);
  }
}

This is usually not a problem, since the function should normally be called as in the following snippets.

 if ($category = db_fetch_object(db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = %d', $id))) {
    $block['subject'] = check_plain($category->title);
    $result = db_query_range('SELECT i.* FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid = i.iid WHERE ci.cid = %d ORDER BY i.timestamp DESC, i.iid DESC', $category->cid, 0, $category->block);
    $read_more = theme('more_link', url('aggregator/categories/' . $category->cid), t("View this category's recent news."));
  }
  while ($item = db_fetch_object($result)) {
    $items[] = theme('aggregator_block_item', $item);
  }

In both the cases, the control statements will not be executed, since for them NULL is equivalent to FALSE.

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.