Lưu ý khi sử dụng $query->fetchAll() (hint: it's optional) trong Drupal 7

Lưu ý khi sử dụng $query->fetchAll() (hint: it's optional) trong Drupal 7

Lưu ý khi sử dụng $query->fetchAll() (hint: it's optional) trong Drupal 7

Something that has come up on one of our projects recently is when to use fetchAll() or equivalents like fetchAllAssoc().

When writing a db_query() or db_select() query, you don't always need to use $query->fetchAll() or $query->fetchAllAssoc() to retrieve the result set; it's optional.

Database queries in Drupal 7 are iterator objects, so you can treat them like an array and loop over the results with foreach(). If you're going to do any processing on the results, it'll be quicker and use much less memory this way.

So for example, this will still work, note the lack of fetchAll():

<?php
// Build and run the query.
$results = db_select('node', 'n')
           ->fields('n', array('nid', 'title'))
           ->condition('status', 0, '!=')
           ->execute();

$nodes = array()
// We can use a foreach loop on the $results object.
foreach ($results as $n) {
  // Each $n is an object.
  $nodes[$n->nid] = $n->title;
}
?>

If you need the result as an array, you can use $results->fetchAssoc() to retrieve one item at a time:

<?php
// Build and run the query.
$results = db_select('node', 'n')
           ->fields('n', array('nid', 'title'))
           ->condition('status', 0, '!=')
           ->execute();

$nodes = array()
// We can use a foreach loop on the $results object.
while ($n = $results->fetchAssoc()) {
  // Each $n is an object.
  $nodes[$n['nid']] = $n['title'];
}
?>

One common use-case for using fetchAll() is so you can use count($results) to find out how many items were returned by the query. To get around this, you can use a count query instead. So:

<?php
// Create the query, but don't execute it yet.
$query = db_select('node', 'n')
         ->fields('n', array('nid', 'title'))
         ->condition('status', 0, '!=');

// This runs a COUNT(*) query to count the number of results.
$count = $query->countQuery()->execute()->fetchField();
// Now run the original query and return the result set.
$results = $query->execute();
?>

More details can be found in the DatabaseStatementInterface API documentation, or in the PHP PDO manual.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Renren

Mạng xã hội Renren - Khắc tinh của Facebook tại China

Việc chính phủ Trung Quốc ra tay chặn Facebook tất nhiên không thể cản nổi bước tiến của những cư dân mạng sinh hoạt trên mạng xã hội lớn nhất thế giới này. Mặc dù cộng đồng người sử dụng Facebook ở Trung Quốc có số lượng khá ít ỏi. Theo thống kê, hiện có khoảng 295.400 người ở Trung Quốc đang sử dụng Facebook. Điều này có nghĩa là cứ 10.000 người sử dụng internet ở Trung Quốc thì có 7 người có tài khoản Facebook đang sử dụng.

5 điểm lưu ý trong quảng cáo Google Adwords năm 2013

5 điểm lưu ý trong quảng cáo Google Adwords năm 2013

Sau nhiều năm phát triển Internet, có thể nói năm 2010 – 2012 là thời gian lan tỏa của công nghệ Internet tại Việt Nam và nhiều nước khác. Do sự phát triển quá mạnh mẽ, bộ máy làm việc của các công cụ tìm kiếm phải cập nhật liên tục, đặc biệt là Google.

Tên miền .xxx của hàng loạt website lớn nhất Việt Nam đã bị đầu cơ

Tên miền .xxx của hàng loạt website lớn nhất Việt Nam đã bị đầu cơ

Dù có giá cao gấp 10 lần tên miền. com, tên miền "người lớn" của hàng loạt các báo điện tử, doanh nghiệp, trang thương mại điện tử lớn nhất tại Việt Nam

Công ty diệt chuột T&C

 

Diet con trung