<?phprequre_once 'Zend/Db/Adapter/Pdo/Mysql.php';$db=new Zend_Db_Adapter_Pdo_Mysql(Array('host'=>'127.0.0.1','username=>'webuser','password'=>'password','dbname'=>'dbname'));?>
<?phprequire_once 'Zend/Db.php';$db=Zend_Db::factory('Pdo_Mysql',array('host'=>'127.0.0.1','username'=>'webuser','password'=>'password','dbname'=>'test'));
<?php$sql='select * from bugs where bug_id=?';$result=$db->fetchAll($sql,2);?>
$data=array('created_on'=>'2007-03-22','bug_description'=>'something wrong','bug_status'=>'new');$db->insert('bugs',$data);$data = array(
'created_on' => new Zend_Db_Expr('CURDATE()'),
'bug_description' => 'Something wrong',
'bug_status' => 'NEW'
);
$db->insert('bugs', $data);
$id=$db->lastInsertId();$id=$db->lastInsertId('bugs','bug-id');$id=$db->lastInsertId('bugs');$id=$db->lastSequenceId('bugs_id_gen');
<?php
$data = array(
'updated_on' => '2007-03-23',
'bug_status' => 'FIXED'
);
$n = $db->update('bugs', $data, 'bug_id = 2');<?php
$data = array(
'updated_on' => '2007-03-23',
'bug_status' => 'FIXED'
);
$where[] = "reported_by = 'goofy'";
$where[] = "bug_status = 'OPEN'";
$n = $db->update('bugs', $data, $where);
// Resulting SQL is:
// UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED'
// WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN')
<?php
$n = $db->delete('bugs', 'bug_id = 3');
<?php
// Start a transaction explicitly.
$db->beginTransaction();
try {
// Attempt to execute one or more queries:
$db->query(...);
$db->query(...);
$db->query(...);
// If all succeed, commit the transaction and all changes
// are committed at once.
$db->commit();
} catch (Exception $e) {
// If any of the queries failed and threw an exception,
// we want to roll back the whole transaction, reversing
// changes made in the transaction, even those that succeeded.
// Thus all changes are committed together, or none are.
$db->rollBack();
echo $e->getMessage();
}
<?php$stmt=$db->query('select * from bugs where reported_by=? and bug_status=?',array('goofy','fixed'));$row=$stmt->fetchAll();var_dump($row);
Zend_Db_Table
Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);// 使用cache缓存器缓存表信息。
class user_account extends Zend_Db_Table_Abstract {
protected $_name='user_account';
protected $_primary='user_id';
protected $_sequence=true;
public function insert(array $data){
if (empty($data['in_time'])) {
$data['in_time'] = timestamp();
}
if(empty($data['user_answer'])){
$data['user_answer']='';
}
return parent::insert($data);
}
public function update(array $data,$where){
$data['in_time'] = timestamp();
return parent::update($data,$where);
}
}
user_account的使用方法
$where = $user->getAdapter()->quoteInto('user_id = ?', 1234);
$user->update($data, $where);