Created
December 20, 2012 13:27
-
-
Save anonymous/4345327 to your computer and use it in GitHub Desktop.
A simple example of using the database in a Band Framework project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Database gets in connection settings from the config file | |
eg. app/resources/config/config.yml | |
database: | |
read: | |
host: localhost | |
port: 3306 | |
user: root | |
password: secret-shhh | |
database: dbName | |
It's actually possible to set up multiple connections (eg, one for reads and one for writes), but typically only read is needed. | |
*/ | |
// Get the database service | |
$db = $this->get('database'); | |
// Fetch a single value from the database | |
$value = $db->one("SELECT value FROM table WHERE id=:id", array('id'=>42)); | |
// Fetch a single row from the database | |
$row = $db->row("SELECT * FROM table WHERE id=:id AND name=:username", | |
array('id' => 42, 'username'=>'Mr Test')); | |
echo $row->name; | |
echo $row->id; | |
// Fetch all the results | |
$all = $db->all("SELECT * FROM table"); | |
if ($all) | |
{ | |
foreach($all as $row) | |
{ | |
echo $row->name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment