One of Drupal 7's major advantages over its precursors is its flexibility with settings and systems. Drupal developers haven't bypassed DB queries, too. In this release the latter have become objective-oriented and are no more attached to some particular database, for the query syntax is now generated by Drupal itself. Thus programmer's task now is simply to indicate the required conditions for the selection. This article deals with Database API analysis which will result in the examples of the changes that occurred to the way the "communication" with DB is performed now.
First of all I'd like to point out some changes that the syntax of site to database >connection has undergone. From the former reading (see below)
php $db_url = 'mysqli://username:password@localhost/basename';
it has transformed into a more readable version
php $databases['default']['default'] = array( 'driver' => 'mysql', 'database' => 'basename', 'username' => 'username', 'password' => 'password', 'host' => 'localhost', );
It's also worth mentioning that Drupal 7 still keeps supporting db_query(), though some minor change having been made hereby, namely, the placeholders changed. One of the suggested options being the use of ? symbol instead of %d, '%s' , in which case the values of such placeholders are indicated in the array after query in the same order as they are placed in the query:
php db_query("SELECT `title` FROM {node} WHERE `uid` = ? AND `type` = ?", array(1, 'page'));
The second option is the one which deals with the usage of the so called "nominal" placeholders. The value replacement is now performed in the associative array. One and the same placeholder cannot be described twice, nor even if it has the same value. Now It's an appropraite practice to have their names assigned the same as of the corresponding table columns whose value they define:
php db_query("SELECT `title` FROM {node} WHERE `uid`= :uid AND `type` = :type", array(':uid' => 1, ':type' => 'page')); ?>