limei 提交于
论坛:
// 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 iteration foreach ($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! ?> // Using the same query from above... $uid = 1; $result = db_query('SELECT n.nid, n.title, n.created FROM {node} n WHERE n.uid = :uid', array(':uid' => $uid)); // Fetch next row as a stdClass object. $record = $result->fetchObject(); // Fetch next row as an associative array. $record = $result->fetchAssoc(); // Fetch data from specific column from next row // Defaults to first column if not specified as argument $data = $result->fetchColumn(1); // Grabs the title from the next row // Retrieve all records into an indexed array of stdClass objects. $result->fetchAll(); // Retrieve all records as stdObjects into an associative array // keyed by the field in the result specified. // (in this example, the title of the node) $result->fetchAllAssoc('title'); // Retrieve a 2-column result set as an associative array of field 1 => field 2. $result->fetchAllKeyed(); // Also good to note that you can specify which two fields to use // by specifying the column numbers for each field $result->fetchAllKeyed(0,2); // would be nid => created $result->fetchAllKeyed(1,0); // would be title => nid // Retrieve a 1-column result set as one single array. $result->fetchCol(); // Column number can be specified otherwise defaults to first column $result->fetchCol($db_column_number); // Count the number of rows $result->rowCount(); ?>
相关文章:
- 登录以发表评论
db_query($query):在当前数据库执行标准查询操作
db_query($query):在当前数据库执行标准查询操作。$query应该是一条正确的sql语句,注意事项有:
1.用正确的占位符取代sql语句中的值: %s, %d, %f, %b (二进制数据请不要直接放在 ''里) 和%%.%s可代替字符串,%d可表示双精度数字,%f为单精度数字,%b为二进制字节流。
2.在sql里把表放在{}里,以免混淆。比如,
- 登录以发表评论
limei 答复于 永久连接mysql_db_query 和mysql_query有什么不同
mysql_db_query 在php4.0.6以后版本就不提倡使用了,且要求不要再使用它了
mysql_db_query('数据名','sql语句');
现由mysql_select_db()和mysql_query() 来取代它
- 登录以发表评论
dashan 答复于 永久连接drupal 7 db_select查询中如何编写自定义为基准进行排序?
- 登录以发表评论
wrp 答复于 永久连接