Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2012 13:27
Show Gist options
  • Save anonymous/4345327 to your computer and use it in GitHub Desktop.
Save anonymous/4345327 to your computer and use it in GitHub Desktop.
A simple example of using the database in a Band Framework project
/*
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