Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

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()

/**
 * COMPAT: escapeSimple - Wraps the supplied string in quotes.
 *
 * This wraps the requested string in quotes, and returns it. It's a bad idea. You should be using
 * prepared queries for this. At some point this will be deprecated and removed.
 */
public function escapeSimple($str = null) {} 

getOne()

/**
 * HELPER: getOne - Returns first result
 *
 * Returns the first result of the first row of the query. Handy shortcut when you're doing
 * a query that only needs one item returned.
 */
public function getOne($sql = null) 
  • No labels