Sarkari Naukri and Naukri

Thursday, 4 April 2013

db_fetch_object not working in drupal 7

<?php// Drupal 7
// Notice the place holders are now done using the same syntax as PDOs (:uid)
// Placeholders also don't need to be quoted anymore.
$uid = 1;$result = db_query('SELECT n.nid, n.title, n.created
FROM {node} n WHERE n.uid = :uid'
, array(':uid' => $uid));// Result is returned as a iterable object that returns a stdClass object on each iterationforeach ($result as $record) {
 
// Perform operations on $record->title, etc. here.
  // in this example the available data would be mapped to object properties:
  // $record->nid, $record->title, $record->created
}


// Same example in Drupal 6$uid = 1;$result = db_query("SELECT n.nid, n.title, n.created
FROM {node} n WHERE n.uid = %d"
, $uid);
while (
$record = db_fetch_object($result)) {
 
// Perform operations on $record->title, etc. here.}// NOTE: db_fetch_object and db_fetch_array have been removed from D7!


?> 



You can use fetchField() to get a single value. Example:

<?php
$node_title
= db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();?>



No comments:

Post a Comment