Database api

Description
1. Database API V 1.0 2. Placeholders   3. DatabaseStatementInterface <ul><li>execute </li></ul><ul><li>fetchAllAssoc…

Please download to get full document.

View again

of 12
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information
Category:

Internet

Publish on:

Views: 0 | Pages: 12

Extension: PDF | Download: 0

Share
Transcript
  • 1. Database API V 1.0
  • 2. Placeholders  
  • 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. 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. 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. 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. 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. 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. 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. 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. All Functions
  • 12. THE END Developed by AragorN ©
  • Related Search
    We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks