SlideShare a Scribd company logo
Drupal 7
Database API
            Andriy Podanenko
  a.podanenko@simbioz.com.ua
Server configuration
PHP 5.0+ (5.3+ recommended)
php5-pdo
php5-pdo_mysql
php5-pdo_pgsql
php5-pdo_sqlite
mysql, postgres…
Apache1.3+ (or php cgi enabled web server)
The start…
php.net/pdo
<?php
abstract class DatabaseConnection extends PDO
…


// Drupal fun…
// Because the other methods don't seem to work right.
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
Class Hierarhy
    abstract class DatabaseConnection extends PDO…




DatabaseConnection
DatabaseConnection::select db_select   (SelectQuery)
DatabaseConnection::delete db_delete   (DeleteQuery)
DatabaseConnection::update db_update   (UpdateQuery)
DatabaseConnection::query db_query     (DatabaseStatementInterface)
DatabaseConnection::insert db_insert   (InsertQuery)
DatabaseConnection::merge db_merge     (MergeQuery)
…
DatabaseStatementInterface



•   execute ($args=array(), $options=array())
•   fetchAllAssoc ($key, $fetch=NULL)
•   fetchAllKeyed ($key_index=0, $value_index=1)
•   fetchAssoc ()
•   fetchCol ($index=0)
•   fetchField ($index=0)
•   getQueryString ()
•   rowCount ()       (UPDATE, DELETE, INSERT only)
Query interface hierarchy
db_select
SELECT name,description FROM taxonomy_term_data AS ttd
WHERE tid LIKE 79
<?php //Drupal
$result = db_select('taxonomy_term_data', 'ttd');
//returned SelectQuery
$ret = $result -> fields('ttd', array('name', 'description'));
//returned SelectQueryInterface
$ret = $result -> condition('tid', 79, 'like');
// returned QueryConditionInterface
$ret = $result -> execute(); // returned SelectQuery object
$data = $ret -> fetchAll(); // fetched data as object from database
Other database (target)
$result = db_select('taxonomy_term_data', 'ttd',
array('target' => 'slave'));
// adding other database at runtime
Database::addConnectionInfo('default', 'slave', array(
'database' => ‘other_db',
'username' => ‘name', 'password' => 'pass',
'host' => 'localhost', 'port' => '5432',
'driver' => 'pgsql'));
DatabaseTransaction (innodb,pgsql)
<?php
function demo_transaction()
$temp = db_transaction();
db_update… // charging account1 +100$
db_update… // charging account2 -100$
db_insert… // creating log entry for that
return TRUE; // automatically commit (no $temp)
} //
$error->handle  try{} catch{}
<?php
function demo_transaction()
$temp = db_transaction();
try {
db_insert… // some ugly code here…
return TRUE; }
catch(Exception $e) {   $temp->rollback();
watchdog(‘demo error', print_r($e, TRUE)}
logging -> DatabaseLog
$demo_log = new DatabaseLog;
$demo_log->start(‘some_key’);
db_update…
db_select…
// Calling functions or any…
$demo_log->end(‘some_key’);
print_r($demo_log->get(‘some_key’), TRUE);
// printed indexed array for all queries…
Drupal 6 – dbtng module (backport)
                           drupal.org/project/dbtng
dbtng_* (full functions list in dbtng.module)
dbtng_query
dbtng_set_active
db_select
• good for support old sites
• multi database setup possibility for Drupal 6
• preparing code for upgrade
Useful links
• api.drupal.org/api/drupal/7
• drupal.org/developing/api/database
• upgrade.boombatower.com/tools/sql/inline
• drupal.org/project/coder
• php.net/pdo
• drupal.org/project/dbtng
• drupal.org/node/310069
Drupal DB api->executed();
               twitter.com/podarok
               facebook.com/podarok
               vkontakte.ru/podarokua
               a.podanenko@simbioz.com.ua
               drupal.org/user/116002
               drupal.ua/user/23



                       http://simbioz.ua

More Related Content

What's hot (20)

PDF
Android Data Persistence
Romain Rochegude
 
ODP
Database Connection With Mysql
Harit Kothari
 
PPT
Persistences
Training Guide
 
PPTX
Mysql
lotlot
 
PDF
Android Data Persistence
Jussi Pohjolainen
 
PDF
Introduction to php database connectivity
baabtra.com - No. 1 supplier of quality freshers
 
PDF
PHP and Mysql
Sankhadeep Roy
 
PPT
MySQL
Gouthaman V
 
PDF
mapserver_install_linux
tutorialsruby
 
PPTX
System performance tuning
Menandro Oba
 
PPT
Database presentation
webhostingguy
 
PDF
Using php with my sql
salissal
 
PPTX
Working with WP_Query in WordPress
topher1kenobe
 
PPTX
PostgreSQL Database Slides
metsarin
 
PDF
Upgrade your javascript to drupal 8
Théodore Biadala
 
KEY
Intro To jQuery In Drupal
Matthew Farina
 
DOCX
Oracle Managed Files
Anar Godjaev
 
PPTX
Database Connectivity in PHP
Taha Malampatti
 
PPTX
1
tristup
 
DOCX
Backup and Recovery
Anar Godjaev
 
Android Data Persistence
Romain Rochegude
 
Database Connection With Mysql
Harit Kothari
 
Persistences
Training Guide
 
Mysql
lotlot
 
Android Data Persistence
Jussi Pohjolainen
 
Introduction to php database connectivity
baabtra.com - No. 1 supplier of quality freshers
 
PHP and Mysql
Sankhadeep Roy
 
mapserver_install_linux
tutorialsruby
 
System performance tuning
Menandro Oba
 
Database presentation
webhostingguy
 
Using php with my sql
salissal
 
Working with WP_Query in WordPress
topher1kenobe
 
PostgreSQL Database Slides
metsarin
 
Upgrade your javascript to drupal 8
Théodore Biadala
 
Intro To jQuery In Drupal
Matthew Farina
 
Oracle Managed Files
Anar Godjaev
 
Database Connectivity in PHP
Taha Malampatti
 
Backup and Recovery
Anar Godjaev
 

Viewers also liked (18)

PPT
Rajppt
Web Explorations
 
ZIP
Drupal Development
Jeff Eaton
 
PPT
An Introduction to Drupal
Compare Infobase Limited
 
PDF
Drupal Step-by-Step: How We Built Our Training Site, Part 1
Acquia
 
PPT
Architecture of Drupal - Drupal Camp
Dipen Chaudhary
 
KEY
What is Drupal? For Site Administrators
Web Explorations
 
PDF
Building Your Agency's Content Strategy Practice
Jeff Eaton
 
PPTX
Using Bootstrap in Drupal 7
Ivan Zugec
 
PPT
Drupal 7 UX –
Roy Scholten
 
KEY
Drupal in Action
Jeff Eaton
 
KEY
What is Drupal - For Content Editors
Web Explorations
 
PDF
Bootstrap Framework and Drupal
Jim Birch
 
PPT
Drupalcamp Estonia - High Performance Sites
Exove
 
PDF
Drupal 7 performance and optimization
Shafqat Hussain
 
PDF
Drupal Step-by-Step: How We Built Our Training Site, Part 2
Acquia
 
PDF
eLearning Proposal
ayounce
 
PDF
Curriculum develop cost time example
Debbi Lewis
 
PDF
3 Ways Online Learning Boosts Employee Engagement
LinkedIn Learning Solutions
 
Drupal Development
Jeff Eaton
 
An Introduction to Drupal
Compare Infobase Limited
 
Drupal Step-by-Step: How We Built Our Training Site, Part 1
Acquia
 
Architecture of Drupal - Drupal Camp
Dipen Chaudhary
 
What is Drupal? For Site Administrators
Web Explorations
 
Building Your Agency's Content Strategy Practice
Jeff Eaton
 
Using Bootstrap in Drupal 7
Ivan Zugec
 
Drupal 7 UX –
Roy Scholten
 
Drupal in Action
Jeff Eaton
 
What is Drupal - For Content Editors
Web Explorations
 
Bootstrap Framework and Drupal
Jim Birch
 
Drupalcamp Estonia - High Performance Sites
Exove
 
Drupal 7 performance and optimization
Shafqat Hussain
 
Drupal Step-by-Step: How We Built Our Training Site, Part 2
Acquia
 
eLearning Proposal
ayounce
 
Curriculum develop cost time example
Debbi Lewis
 
3 Ways Online Learning Boosts Employee Engagement
LinkedIn Learning Solutions
 
Ad

Similar to Drupal 7 database api (20)

PDF
Drupal - dbtng 25th Anniversary Edition
ddiers
 
PPTX
Drupal II: The SQL
ddiers
 
PPTX
Drupal 8 database api
Viswanath Polaki
 
PPTX
Drupal7 dbtng
Nicolas Leroy
 
PPTX
DrupalCamp Foz - Novas APIs Drupal 7
chuvainc
 
PDF
Database API, your new friend
kikoalonsob
 
PDF
PHP Data Objects
Wez Furlong
 
PPT
Php Data Objects
hiren.joshi
 
PDF
Syntactic sugar in Postgre SQL
Antony Abramchenko
 
PPTX
Syntactic sugar in postgre sql
Antony Abramchenko
 
PPT
Introducing PHP Data Objects
webhostingguy
 
PPT
Working with databases in Perl
Laurent Dami
 
PDF
Sqlite perl
Ashoka Vanjare
 
DOCX
Belajar php dengan database firebird
Ali Muntaha
 
PPTX
Php and database functionality
Sayed Ahmed
 
PPTX
Php and database functionality
Sayed Ahmed
 
PPTX
PHP and database functionality
Sayed Ahmed
 
PPTX
Doctrine 2.0 Enterprise Persistence Layer for PHP
Guilherme Blanco
 
PDF
The History of PHPersistence
Hugo Hamon
 
PPTX
working with PHP & DB's
Hi-Tech College
 
Drupal - dbtng 25th Anniversary Edition
ddiers
 
Drupal II: The SQL
ddiers
 
Drupal 8 database api
Viswanath Polaki
 
Drupal7 dbtng
Nicolas Leroy
 
DrupalCamp Foz - Novas APIs Drupal 7
chuvainc
 
Database API, your new friend
kikoalonsob
 
PHP Data Objects
Wez Furlong
 
Php Data Objects
hiren.joshi
 
Syntactic sugar in Postgre SQL
Antony Abramchenko
 
Syntactic sugar in postgre sql
Antony Abramchenko
 
Introducing PHP Data Objects
webhostingguy
 
Working with databases in Perl
Laurent Dami
 
Sqlite perl
Ashoka Vanjare
 
Belajar php dengan database firebird
Ali Muntaha
 
Php and database functionality
Sayed Ahmed
 
Php and database functionality
Sayed Ahmed
 
PHP and database functionality
Sayed Ahmed
 
Doctrine 2.0 Enterprise Persistence Layer for PHP
Guilherme Blanco
 
The History of PHPersistence
Hugo Hamon
 
working with PHP & DB's
Hi-Tech College
 
Ad

More from Andrii Podanenko (20)

PDF
Глобальні навчальні дні друпал
Andrii Podanenko
 
PPTX
природна і економна дорожня карта для переходу команди розробки на тест центр...
Andrii Podanenko
 
PDF
Головні Принципи Автоматизації
Andrii Podanenko
 
PDF
Docker SQL Continuous Integration Flow
Andrii Podanenko
 
PDF
Drupal 8 DevOps . Profile and SQL flows.
Andrii Podanenko
 
PPTX
Drupal 8 configuration development flow
Andrii Podanenko
 
PPTX
MoldCamp - multidimentional testing workflow. CIBox.
Andrii Podanenko
 
PDF
CIbox - OpenSource solution for making your #devops better
Andrii Podanenko
 
PDF
DrupalCon Los Angeles - Continuous Integration Toolbox
Andrii Podanenko
 
PPTX
Drupal Continuous Integration Workflow
Andrii Podanenko
 
PDF
Сейчас самое время...ЖИТЬ!
Andrii Podanenko
 
PDF
Start using vagrant now!
Andrii Podanenko
 
PDF
Live deployment, ci, drupal
Andrii Podanenko
 
PDF
Drupal contrib module maintaining
Andrii Podanenko
 
PDF
Drupal codesprint kyiv drupal cafe 07.02.2013
Andrii Podanenko
 
PDF
Drupal 8 what to wait from
Andrii Podanenko
 
PDF
Do + ldo for developers(full)
Andrii Podanenko
 
PDF
Диалоговый интенсив 2012 карпаты
Andrii Podanenko
 
PDF
психолог в социальных медиа
Andrii Podanenko
 
PDF
Ubercart7 views catalog ru
Andrii Podanenko
 
Глобальні навчальні дні друпал
Andrii Podanenko
 
природна і економна дорожня карта для переходу команди розробки на тест центр...
Andrii Podanenko
 
Головні Принципи Автоматизації
Andrii Podanenko
 
Docker SQL Continuous Integration Flow
Andrii Podanenko
 
Drupal 8 DevOps . Profile and SQL flows.
Andrii Podanenko
 
Drupal 8 configuration development flow
Andrii Podanenko
 
MoldCamp - multidimentional testing workflow. CIBox.
Andrii Podanenko
 
CIbox - OpenSource solution for making your #devops better
Andrii Podanenko
 
DrupalCon Los Angeles - Continuous Integration Toolbox
Andrii Podanenko
 
Drupal Continuous Integration Workflow
Andrii Podanenko
 
Сейчас самое время...ЖИТЬ!
Andrii Podanenko
 
Start using vagrant now!
Andrii Podanenko
 
Live deployment, ci, drupal
Andrii Podanenko
 
Drupal contrib module maintaining
Andrii Podanenko
 
Drupal codesprint kyiv drupal cafe 07.02.2013
Andrii Podanenko
 
Drupal 8 what to wait from
Andrii Podanenko
 
Do + ldo for developers(full)
Andrii Podanenko
 
Диалоговый интенсив 2012 карпаты
Andrii Podanenko
 
психолог в социальных медиа
Andrii Podanenko
 
Ubercart7 views catalog ru
Andrii Podanenko
 

Recently uploaded (20)

PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 

Drupal 7 database api

  • 2. Server configuration PHP 5.0+ (5.3+ recommended) php5-pdo php5-pdo_mysql php5-pdo_pgsql php5-pdo_sqlite mysql, postgres… Apache1.3+ (or php cgi enabled web server)
  • 3. The start… php.net/pdo <?php abstract class DatabaseConnection extends PDO … // Drupal fun… // Because the other methods don't seem to work right. $driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
  • 4. Class Hierarhy abstract class DatabaseConnection extends PDO… DatabaseConnection DatabaseConnection::select db_select (SelectQuery) DatabaseConnection::delete db_delete (DeleteQuery) DatabaseConnection::update db_update (UpdateQuery) DatabaseConnection::query db_query (DatabaseStatementInterface) DatabaseConnection::insert db_insert (InsertQuery) DatabaseConnection::merge db_merge (MergeQuery) …
  • 5. DatabaseStatementInterface • execute ($args=array(), $options=array()) • fetchAllAssoc ($key, $fetch=NULL) • fetchAllKeyed ($key_index=0, $value_index=1) • fetchAssoc () • fetchCol ($index=0) • fetchField ($index=0) • getQueryString () • rowCount () (UPDATE, DELETE, INSERT only)
  • 7. db_select SELECT name,description FROM taxonomy_term_data AS ttd WHERE tid LIKE 79 <?php //Drupal $result = db_select('taxonomy_term_data', 'ttd'); //returned SelectQuery $ret = $result -> fields('ttd', array('name', 'description')); //returned SelectQueryInterface $ret = $result -> condition('tid', 79, 'like'); // returned QueryConditionInterface $ret = $result -> execute(); // returned SelectQuery object $data = $ret -> fetchAll(); // fetched data as object from database
  • 8. Other database (target) $result = db_select('taxonomy_term_data', 'ttd', array('target' => 'slave')); // adding other database at runtime Database::addConnectionInfo('default', 'slave', array( 'database' => ‘other_db', 'username' => ‘name', 'password' => 'pass', 'host' => 'localhost', 'port' => '5432', 'driver' => 'pgsql'));
  • 9. DatabaseTransaction (innodb,pgsql) <?php function demo_transaction() $temp = db_transaction(); db_update… // charging account1 +100$ db_update… // charging account2 -100$ db_insert… // creating log entry for that return TRUE; // automatically commit (no $temp) } //
  • 10. $error->handle  try{} catch{} <?php function demo_transaction() $temp = db_transaction(); try { db_insert… // some ugly code here… return TRUE; } catch(Exception $e) { $temp->rollback(); watchdog(‘demo error', print_r($e, TRUE)}
  • 11. logging -> DatabaseLog $demo_log = new DatabaseLog; $demo_log->start(‘some_key’); db_update… db_select… // Calling functions or any… $demo_log->end(‘some_key’); print_r($demo_log->get(‘some_key’), TRUE); // printed indexed array for all queries…
  • 12. Drupal 6 – dbtng module (backport) drupal.org/project/dbtng dbtng_* (full functions list in dbtng.module) dbtng_query dbtng_set_active db_select • good for support old sites • multi database setup possibility for Drupal 6 • preparing code for upgrade
  • 13. Useful links • api.drupal.org/api/drupal/7 • drupal.org/developing/api/database • upgrade.boombatower.com/tools/sql/inline • drupal.org/project/coder • php.net/pdo • drupal.org/project/dbtng • drupal.org/node/310069
  • 14. Drupal DB api->executed();  twitter.com/podarok  facebook.com/podarok  vkontakte.ru/podarokua  [email protected]  drupal.org/user/116002  drupal.ua/user/23 http://simbioz.ua