PDO Cheatsheet

 

Introduction

FreePBX has deprecated the use of PEAR and is transitioning to PDO. All new code should be written using PDO.

PDO Resources

 The Object

 You should no longer use 

function foo($bar){     global $db;     ....

To use PDO outside a BMO class you can create the object as needed.

function foo($bar){     $dbh = \FreePBX::Database();     ....

Note you can use $db inside of the scope of a function. If you try to use is outside of a function it will collide with the global $db and break stuff in a nonsensical way.

Within a Class you can add the handle to your construct then reference it anywhere in your class as $this->Database;

public function __construct($freepbx = null) {     if ($freepbx == null) {         throw new Exception("Not given a FreePBX Object");     }     $this->FreePBX = $freepbx;     $this->db = $freepbx->Database;     ....

Examples

Fetch all items as an associated array or false

Fetch all items in an associated array by id or false

Insert an Item and return id or false

Get one row as an associative array or false;

Exceptions

You may wish to allow some exceptions and not have them bomb everything. You can catch exceptions in a try catch.

Exception code

Description

Exception code

Description

42S21

Column Exists

42S02

Table does not exist

Return to Documentation Home I Return to Sangoma Support