Databases Reference
In-Depth Information
information (for example, a database connection or view) and be made available
in your Web application when required.
Example 3-11 Registering database
$db = new Database($conn);
Zend::register('db', $db);
For a database driven application, the database registration code as shown in
Example 3-11 should be placed in the bootstrap file located under framework's
www directory. Once registered, the database object can be accessed anywhere
just by making it available through the registry:
$db = Zend::registry('db');
Now, our simple database class implements the utility methods to add, update,
and query the movie database as shown in Example 3-12.
Example 3-12 Database class
<?php
class Database
{
private $_db;
public function __construct($conn)
{
$this → _db = $conn;
}
public function addMovie($movie_name)
{
$row = array('name' => $movie_name);
$table = 'movie_names';
$rows_affected = $this → _db → insert($table, $row);
}
public function updateVote($vote)
{
$sql = "UPDATE movie_names set vote = vote+1 where name='$vote'";
$this → _db → query($sql);
}
public function showVote()
{
$sql = "SELECT sum(vote) as sum from movie_names";
return $this → _db → fetchRow($sql);
Search WWH ::




Custom Search