-
Notifications
You must be signed in to change notification settings - Fork 0
DataSource:Database
###Description
Contains the 'Database' class, which contains functions for working with a database using PHP Data Objects (PDO).
###Dependencies
Standard Libraries
- PDO
- PDOException
####__construct() Constructor for the 'Database' class, which creates a Database object
Params
- string[] $settings Connection Settings
- ['user'] User Name
- ['password'] Password
- ['driver'] Database Driver
- ['host'] Hostname
- ['port'] Port Number
- ['schema'] Schema Name
- ['auto_connect'] Auto-Connect Flag
- ['pdo_options'][] PDO Options (Array)
Notes
'auto_connect' is recommended when you need to grab data from a specific database on most or all pages. For example, THINK would need to automatically connect to the 'thinkahead' schema since it relies on that for everything. However, THINK may want to connect to Pass to retrieve data in some cases, but it doesn't rely on that connection to work properly.
####connect() Opens a connection to the database
Params
- boolean $dieOnError Kills script execution on connection error, if true (default: false)
Return
boolean True if the connection was successful, False if a failure occurs
Example
Connect to the 'thinker' database
$_DB['thinker']->connect();
- Connect to the 'thinker' database, and kill the script if unsuccessful*
$_DB['thinker']->connect(true);
####doQuery() Executes a query against the database
Params
- string $query SQL Query to execute
- mixed[] $data Query Parameters
Return
boolean True if the query was executed successfully, False if a failure occurs
Example
Delete row with ID '1' from thinker.Users
$query = "DELETE FROM thinker.Users WHERE ID = :id LIMIT 1";
$params = array(':id' => 1);
$_DB['thinker']->doQuery($query, $params);
Notes
This should only be used for INSERT, DELETE, and UPDATE queries. It will not return data if SELECT is used.
####doQueryAns() Executes a query against the database that returns a single value
Params
- string $query SQL Query to execute
- mixed[] $data Query Parameters
Return
mixed Query Result
Example
Get Name of user with ID '1' from thinker.Users
$query = "SELECT Name FROM thinker.Users WHERE ID = :id LIMIT 1";
$params = array(':id' => 1);
$result = $_DB['thinker']->doQueryAns($query, $params);
####doQueryArr() Executes a query against the database that returns multiple values
Params
- string $query SQL Query to execute
- mixed[] $data Query Parameters
Return
mixed[] Query Result
Example
Get all Names and Emails of users from thinker.Users who are from PA
$query = "SELECT Name, Email FROM thinker.Users WHERE State = :state";
$params = array(':state' => 'PA');
$results = $_DB['thinker']->doQueryArr($query, $params);
Notes
foreach() loops are best for working with this data. The following syntax is recommended, based on the example:
foreach($results as $r)
{
list($name, $email) = $r;
echo "Name: $name, Email: $email\n";
}
This will print a list of names and emails from the $results array.
####doQueryOne() Executes a query against the database that returns a single row's values
Params
- string $query SQL Query to execute
- mixed[] $data Query Parameters
Return
mixed[] Query Result
Example
Get the Name and Email of the user with ID '1' from thinker.Users
$query = "SELECT Name, Email FROM thinker.Users WHERE ID = :id LIMIT 1";
$params = array(':id' => 1);
$result = $_DB['thinker']->doQueryOne($query, $params);
Usage:
echo "Name: {$result['Name']}, Email: {$result['Email']}";
THINKer Framework + Libraries Copyright © Cory Gehr. All rights reserved.