Drupal 7 introduces a completely new database API, utilizing a number of dynamic query builders and formal prepared statements. The following Drupal 6 functions and hooks were removed: db_affected_rows(), db_distinct_field(), db_error(), db_last_insert_id(), db_placeholders(), db_lock_table(), db_prefix_tables(), db_result(), db_fetch_*(), db_version(), db_rewrite_sql(), hook_db_rewrite_sql(), pager_query(), tablesort_sql(), and others.
For full information, read the Database API guide. A few common examples of Drupal 6 to 7 conversion are also covered below.
Normal SELECT queries:
<?php
// Drupal 6
$result = db_query("SELECT nid, title FROM {node} WHERE uid = %d AND type IN (" . db_placeholders(array('page', 'story'), 'varchar') . ")", 5, 'page', 'story');
// Drupal 7
$result = db_query("SELECT nid, title FROM {node} WHERE uid = :uid AND type IN (:type)", array(
':uid' => 5,
':type' => array('page', 'story'),
));
?>
<?php
// Drupal 6
while ($record = db_fetch_object($result)) {
// Do stuff with $record, which is an object}
// Drupal 7
foreach ($result as $record) {
// Do stuff with $record, which is an object}
?>
<?php
// Drupal 6
db_query("INSERT INTO {mytable} (intvar, stringvar, floatvar) VALUES (%d, '%s', %f)", 5, 'hello world', 3.14);$id = db_last_insert_id();
// Drupal 7
$id = db_insert('mytable')
->fields(array(
'intvar' => 5,
'stringvar' => 'hello world',
'floatvar' => 3.14,
))
->execute();
?>
<?php
// Drupal 6
db_query("UPDATE {node} SET title='%s', status=%d WHERE uid=%d", 'hello world', 1, 5);
// Drupal 7
db_update('node')
->fields(array('title' => 'hello world', 'status' => 1))
->condition('uid', 5)
->execute();
?>
<?php
// Drupal 6
db_query("DELETE FROM {node} WHERE uid=%d AND created < %d", 5, REQUEST_TIME - 3600);
// Drupal 7
db_delete('node')
->condition('uid', 5)
->condition('created', REQUEST_TIME - 3600, '<')
->execute();
?>
Nice details about drupal changes from 6 to 7.
ReplyDeleteDrupal Development Company
I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.It can be helpful for people who wants to know more about app Modernization services.
ReplyDelete