over 9 years ago
Drupal database abstraction layer allow's the use of different database servers using the same code base i.e it provides with the ability to support multiple database servers easily. The system is built a top PHP's PHP Data Objects database API and inherits much of its syntax.
with select query we can use:
- execute($arg=array(), $option = array())
- fetchAllAssoc($key, fetch=null)
- fetchAllKeyed($keyindex=0, $valueindex=1)
- fetchAssoc()
- fetchCol($inde=0)
- fetchField($inde=0)
- getQueryString()
- rowCount()
execute($arg=array(), $option = array()) fetchAllAssoc($key, fetch=null) fetchAllKeyed($keyindex=0, $valueindex=1) fetchAssoc() fetchCol($inde=0) fetchField($inde=0) getQueryString() rowCount()
Some very basic example are as under:
Basis SQL Select query:
Select in Drupal 7 way :
- <?php
- $query = db_select('node', 'n');
- $query->fields('n', array('name', 'n.description','created'));
- $query->condition('n.uid', $uid, '=');
- $result = $query->execute();
- //count the result
- $datacount = $result->rowCount();
- if($datacount > 0){
- //Fetch the data as object from database
- $data = $result->fetchAll();
- }
- ?>
<?php $query = db_select('node', 'n'); $query->fields('n', array('name', 'n.description','created')); $query->condition('n.uid', $uid, '='); $result = $query->execute(); //count the result $datacount = $result->rowCount(); if($datacount > 0){ //Fetch the data as object from database $data = $result->fetchAll(); } ?>
Basis SQL Insert query:
Insert in Drupal 7 way :
Basis SQL Update query:
Update in Drupal 7 way :
Basis SQL Delete query:
Drupal 7 Way :
0 Comment(s)