Bekijk de implementatie van Zend_Db
, en in het bijzonder Zend_Db_Select
. U kunt er zelfs voor kiezen om dat te gebruiken in plaats van uw eigen te ontwikkelen. Voorbeelden:
//connect to a database using the mysqli adapter
//for list of other supported adapters see
//http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.adapter-notes
$parameters = array(
'host' => 'xx.xxx.xxx.xxx',
'username' => 'test',
'password' => 'test',
'dbname' => 'test'
);
try {
$db = Zend_Db::factory('mysqli', $parameters);
$db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
echo $e->getMessage();
die('Could not connect to database.');
} catch (Zend_Exception $e) {
echo $e->getMessage();
die('Could not connect to database.');
}
//a prepared statement
$sql = 'SELECT * FROM blah WHERE id = ?';
$result = $db->fetchAll($sql, 2);
//example using Zend_Db_Select
$select = $db->select()
->from('blah')
->where('id = ?',5);
print_r($select->__toString());
$result = $db->fetchAll($select);
//inserting a record
$row = array('name' => 'foo',
'created' => time()
);
$db->insert('blah',$row);
$lastInsertId = $db->lastInsertId();
//updating a row
$data = array(
'name' => 'bar',
'updated' => time()
);
$rowsAffected = $db->update('blah', $data, 'id = 2');