Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Database API V 1.0
Placeholders  
DatabaseStatementInterface <ul><li>execute </li></ul><ul><li>fetchAllAssoc </li></ul><ul><li>fetchAllKeyed </li></ul><ul><...
SELECT All <ul><li>Drupal 6 </li></ul><ul><li>$result = db_query(“SELECT * FROM {node}”); </li></ul><ul><li>Drupal 7 </li>...
SELECT + Placeholders <ul><li>Drupal 6 </li></ul><ul><li>$result = db_query(“SELECT `nid`, `title` FROM {node}  </li></ul>...
SELECT + WHERE <ul><li>Drupal 6 </li></ul><ul><li>$result = db_query(“SELECT `type`, `title` FROM {node}  </li></ul><ul><l...
SELECT + JOIN + GROUP + ORDER $query = db_select(‘node’, ‘n’); $query->join(‘users’, ‘u’, ‘n.uid = u.uid’);  $query->group...
INSERT <ul><li>Drupal 6  </li></ul><ul><li>db_query(&quot;INSERT INTO {node} (`title`, `uid`, `created`)  </li></ul><ul><l...
UPDATE <ul><li>Drupal 6 </li></ul><ul><li>db_query(“UPDATE {node}  </li></ul><ul><li>SET `title` = ‘%s’, `status` = %d  </...
DELETE <ul><li>Drupal 6 </li></ul><ul><li>db_query(“DELETE FROM {node}  </li></ul><ul><li>WHERE `uid` = %d AND `created` <...
All Functions
THE END Developed by AragorN ©
Upcoming SlideShare
Loading in …5
×

Database api

2,328 views

Published on

Published in: Internet, Technology, Business
  • Be the first to comment

  • Be the first to like this

Database api

  1. 1. Database API V 1.0
  2. 2. Placeholders  
  3. 3. DatabaseStatementInterface <ul><li>execute </li></ul><ul><li>fetchAllAssoc </li></ul><ul><li>fetchAllKeyed </li></ul><ul><li>fetchAssoc </li></ul><ul><li>fetchCol </li></ul><ul><li>fetchField </li></ul><ul><li>getQueryString </li></ul><ul><li>rowCount </li></ul>
  4. 4. SELECT All <ul><li>Drupal 6 </li></ul><ul><li>$result = db_query(“SELECT * FROM {node}”); </li></ul><ul><li>Drupal 7 </li></ul><ul><li>$result = db_select(‘node’, ‘n’)      ->fields(‘n’)     ->execute()     ->fetchAssoc(); </li></ul>
  5. 5. SELECT + Placeholders <ul><li>Drupal 6 </li></ul><ul><li>$result = db_query(“SELECT `nid`, `title` FROM {node} </li></ul><ul><li>WHERE `uid` = %d AND `type` = '%s‘”, 1, ‘page’); </li></ul><ul><li>Drupal 7 </li></ul><ul><li>$result = db_query(“SELECT `nid`, `title` FROM {node} </li></ul><ul><li>WHERE `uid` = :uid AND `type` = :type”, </li></ul><ul><li>array( </li></ul><ul><li>‘ :uid’ => 1, </li></ul><ul><li>‘ :type’ => ‘page’, </li></ul><ul><li>)); </li></ul>
  6. 6. SELECT + WHERE <ul><li>Drupal 6 </li></ul><ul><li>$result = db_query(“SELECT `type`, `title` FROM {node} </li></ul><ul><li>WHERE `nid` = %d AND `status` > %d AND `uid` IN ('&quot; . </li></ul><ul><li>implode(&quot;','&quot;, $uid) . &quot;')”, </li></ul><ul><li>$node->nid, 0, $uid = array(1, 5, 7)); </li></ul><ul><li>Drupal 7 </li></ul><ul><li>$result = db_select(‘node’, ‘n’)   ->fields(‘n‘, array(‘type’, ‘tittle’,))   ->condition(‘nid’, $node->nid, ‘=‘)   ->condition(‘status’, 0, ‘>’)   ->condition(‘uid’, array(1, 5, 7), ‘IN’)   ->execute()   ->fetchAssoc(); </li></ul>
  7. 7. SELECT + JOIN + GROUP + ORDER $query = db_select(‘node’, ‘n’); $query->join(‘users’, ‘u’, ‘n.uid = u.uid’); $query->groupBy(‘u.uid’); $query->fields(‘n’, array(‘title’, ’created’))    ->fields(‘u’, array(‘name’))    ->orderBy(‘created’, ‘DESC’)    ->range(0, 10);     $result = $query->execute(); while ($record = $result->fetchAssoc()) {     print_r ($record);  }
  8. 8. INSERT <ul><li>Drupal 6 </li></ul><ul><li>db_query(&quot;INSERT INTO {node} (`title`, `uid`, `created`) </li></ul><ul><li>VALUES (‘%s’, %d, ‘%s’)&quot;, ‘Title’, 1, REQUEST_TIME); </li></ul><ul><li>$id = db_last_insert_id();   </li></ul><ul><li>Drupal 7 </li></ul><ul><li>$id = db_insert(‘node’)->fields(array( </li></ul><ul><li>‘ title’ => ‘Title’, </li></ul><ul><li>‘ uid’ => 1, </li></ul><ul><li>‘ created’ => REQUEST_TIME, </li></ul><ul><li>))->execute(); </li></ul>
  9. 9. UPDATE <ul><li>Drupal 6 </li></ul><ul><li>db_query(“UPDATE {node} </li></ul><ul><li>SET `title` = ‘%s’, `status` = %d </li></ul><ul><li>WHERE `uid` = %d”, ‘New title’, 1, 5);   </li></ul><ul><li>Drupal 7 </li></ul><ul><li>db_update(‘node’)->fields(array( </li></ul><ul><li>‘ title’ => ‘New title’, </li></ul><ul><li>‘ status’ => 1 </li></ul><ul><li>))->condition(‘uid’, 5)->execute(); </li></ul>
  10. 10. DELETE <ul><li>Drupal 6 </li></ul><ul><li>db_query(“DELETE FROM {node} </li></ul><ul><li>WHERE `uid` = %d AND `created` < %d”, </li></ul><ul><li>5, time() - 3600);  </li></ul><ul><li>Drupal 7 </li></ul><ul><li>db_delete(‘node’) </li></ul><ul><li>->condition(‘uid’, 5) </li></ul><ul><li>->condition(‘created’, time() - 3600, ‘<‘) </li></ul><ul><li>->execute(); </li></ul>
  11. 11. All Functions
  12. 12. THE END Developed by AragorN ©

×