Database

As we move FreePBX away from using the PearDB object this article can and will largely change. Be warned.

 

This is a PDO Object, which is already connected to the FreePBX Database.

//Old PearDB Style, (Not Recommended) $array = FreePBX::Database()->sql('SELECT * FROM admin');    //New PDO Style $sql = 'SELECT * FROM admin'; $sth = FreePBX::Database()->prepare($sql); $sth->execute(); $array = $sth->fetchAll(PDO::FETCH_ASSOC);

Alternatively you can connect to another database to manipulate it's data

$database = FreePBX::Database('mysql:dbname=testdb;host=127.0.0.1','dbuser','dbpass');   //Old PearDB Style, (Not Recommended) $array = $database->sql('SELECT * FROM admin');   //New PDO Style $sql = 'SELECT * FROM admin'; $sth = $database->prepare($sql); $sth->execute(); $array = $sth->fetchAll(PDO::FETCH_ASSOC);

sql()

/**  * COMPAT: Queries Database using PDO  *  * This is a FreePBX Compatibility hook for the global 'sql' function that  * previously used PEAR::DB  *  * @param $sql string SQL String to run  * @param $type string Type of query  * @param $fetchmode int One of the PDO::FETCH_ methos (see http://www.php.net/manual/en/pdo.constants.php for info)  */ public function sql($sql = null, $type = "query", $fetchmode = PDO::FETCH_BOTH) {}

escapeSimple()

getOne()

Return to Documentation Home I Return to Sangoma Support