Zend_DB( 笑话大全网技术收集www.xiao688.com)
1.使用适配器连接数据库
使用适配器的构造函数
<?php
     requre_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'));
?>
使用Zend_Db Factory连接数据库
 Factory 模式允许在运行时初始化一个对象,他在对一个手工创建的对象进行反馈的时候调用Factory模式。
<?php
require_once 'Zend/Db.php';
$db=Zend_Db::factory('Pdo_Mysql',array(
     'host'=>'127.0.0.1',
     'username'=>'webuser',
     'password'=>'password',
     'dbname'=>'test'
));
适配器参数
host:database host
username:access username
password:access password
dbname:the database name to use
port:the database server port
options:
driver_options:
adapterNamespace:当你使用的是一个自定义的适配 器的时候,这个属性初始化了适配器的类名
2.数据获取模式
Zend_Db::FETCH_ASSOC
Zend_Db::FETCH_NUM
Zend_Db::FETCH_BOTH
Zend_Db::FETCH_COLUMN
Zend_Db::FETCH_OBJ
fetchAll()====> 在一个步骤中运行sql select查询并且获取他的查询结果
 
<?php
$sql='select * from bugs where bug_id=?';
$result=$db->fetchAll($sql,2);
?>
fetchAssoc()====>和fetchAll类 似,返回的是关联数组的结果
fetchCol()=====>和fetchAll类 似,不过只返回第一列的值。
fetchRow()=====>只返回第一行的值
fetchPairs()====>返回一个值-》结果的 数组,第一列作为值,第二列作为结果。
fetchOne()====>相当于fetchCol和 fetchRow的结合,只返回的是第一行第一列的一个值。
3.写如更改到数据库
插入数据
$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 ();
}

创建一个statement
创建一个sqlstatement对象使用query()函数
<?php
$stmt=$db->query('select * from bugs where reported_by=? and bug_status=?',array('goofy','fixed'));
$row=$stmt->fetchAll();
var_dump($row);
 
fetch()从数据集中获得单独的一行
 有三个参数
fetch style 数据获取模式
cursor orientation 默认的是Zend_DB::FETCH_ORI_NEXT,意味着每一次调用fetch()
返回下一行的结果集,按照rdbms中的顺序。
offset  如果cursor orientation设置为Zend_DB::FETCH_ORI_ABS,那么offset数值就是返回的顺序数。
当所有的数据都被返回之后fetch()函数将返回 false.
fetchAll()函数相当于fetch()函数的循环模 式,他价格获得所有合适的结果集,并且存储在一个数组中。
fetchAll()的两个参数:fetch style。当fetchstyle为fetch_Col的时候,第二个参数设置的是返回第几列的值
fetchColumn()返回某一列的值,有一个参数指定的 是需要返回的列的索引
fetchObject()返回值作为对象来处理。
适配器的select创建sql语句
$select=$db->select();
$select->from();
$select->where();
$select->order();
$select->limit(); 
 $stmt=$db->query($select)
$result=$stmt->fetchAll()
使用$select->__toString()可以输出 sql语句;
-昱志 陈 08-5-20 上午9:58
 
$db->select()->from('user_account');
等效于sql语句select * from user_account;
$db->select()->from('user_account',array('user_mail','answer'=>'user_answer'));
等效于sql语句select user_mail,user_answer as answer from user_account;
$db->select()->from('user_account')->where('user_id>1');
等效于sql语句select * from user_account where user_id>1;
$db->select()->from('user_account')->where('user_id>1')->Orwhere("user_name='163'");
等效于sql语句select * from user_account where user_id>1 or user_name='163';
$db->select()->from('user_account')->where('user_id>1')->where("user_name='163'");
等效于sql语句select * from user_account where user_id>1 and user_name= '163';
$db->select()->from('user_account')->where('user_id>1')->order(array('user_id desc','user_email asc'))
 等效于sql语句select * from user_account where user_id>1 order by user_id desc ,user_email asc;
$db->select()->from(array('ua'=>'user_account'),array('user_password','user_mail'))->joinleft(array('ui'=>'user_info'),'ui.id=ua.id',array('description))->orderby(array('user_id desc'));
等效于sql语句select ua.user_mail,ua.user_password,ui.user_info from user_account as ua,user_info as ui where ui.id=ua.id order by user_id desc;
 

Zend_Db_Table
 需要创建自己的table
 Zend_Db_Table_Abstract::setDefaultAdapter($db);// 选择默认适配器
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);
 }
}
-昱志 陈 08-5-20 上午10:01
 

user_account的使用方法
$user=new user_account()//创建了一个针对表user_account的对象$user;
/**********创建一行方法 1*********************/ 
$user->createRow();
$user->user_name='alphachen';
$user->user_mail='ialpha@163.com';
$user->user_question=0;
$user->user_answer="hello world";
$user->save();
/***********创建一行方法 2********************/
$data=array(
        'user_name'=>'alphachen',
         'user_mail'=>'ialpha@163.com'
    );
$user->insert($data);
/************查找 一行**********************/
按照主键查找
$user->find(1);//查找user_id=1 的行
$user->find(array(1,2,3));// 查找user_id=1或者2或者3的行
 $select=$user->select()//建议 使用select选择器
$rows=$user-> fetchAll($select);//获得返回值
/*************更新数据方法 1***************************/
$data=array(
        'user_name'=>'alphachen',
         'user_mail'=>'ialpha@163.com'
    );
$where = $user->getAdapter()->quoteInto('user_id = ?', 1234);
$user->update($data, $where);
/***************更新数据方法 2***************************/
 $row=$user->fetchRow($user->select()->where('user_id=123');
$row->user_name='alphachen';
$row->save()
/**************删除数 据*****************************/
 $user->delete("user_id=12");