现在的位置: 首页 > 综合 > 正文

Zend_Db二

2012年09月21日 ⁄ 综合 ⁄ 共 3282字 ⁄ 字号 评论关闭
<?php

set_include_path ( 'D:\apache\htdocs\zend\\' ); //zend 路径

 

 

require_once 'zend/db.php';

//数据库参数配置

$params = array ('host' => '127.0.0.1', 'username' => 'root', 'password' => 'root', 'dbname' => 'test1' );

$db = Zend_Db::factory ( 'pdo_mysql', $params );

 

//zend_db_table用法=========================================================================================================

 

 

require_once 'zend/db/table.php';

require_once 'zend/debug.php';

 

Zend_Db_Table::setDefaultAdapter ( $db ); //为所有的zen_db_table对象设置默认数据库链接

 

 

class round_table extends Zend_Db_Table {

}

; //继承抽象类 类名称为数据库表名称

 

 

$table = new round_table ();

 

//插入数据----------------------------------------------------------------------------

$date = array (id => null, 'name' => 'tom', 'age' => 25 );

$table->insert ( $date );

 

//通过主键查找数据(zf默认主键id)-------------------------------------------------------

$rows = $table->find ( 113 );

zend_debug::dump ( $rows, 'zend_debug:', 0 );

 

//删除数据-------------------------------------------------------------------

//$where = $table->getAdapter()->quoteinto('name=?','tom');

////$where = $table->quoteinto('name=?','tom');

//$table->delete($where);

 

 

//更新数据----------------------------------------------------------------------------

$set = array ('name' => 'james', 'age' => 55 );

$where = $table->getAdapter ()->quoteinto ( 'name=?', 'tom' );

$table->update ( $set, $where );

 

//获取一组数据----------------------------------------------------------------------------

$sql = $table->getAdapter ()->quoteinto ( 'name=?', 'james' );

$rows = $table->fetchAll ( $sql );

//zend_debug::dump($rows,'',1);

 

 

//获取一组数据2

$where = array ('name= ?' => 'tom', 'name=?' => 'ben' );

$rows = $table->fetchAll ( $where );

//var_dump($rows);

 

 

//获取数据3

$where = $table->getAdapter ()->quoteinto ( 'name = ?', 'ben' );

$order = 'age';

$count = 5;

$offset = 0;

$rows = $table->fetchAll ( $where, $order, $count, $offset ); //参数按正常sql查询顺序排列

//zend_debug::dump($rows);

 

 

//获取表属性---------------------------------------

$table->info ();

 

//zend_db_table_rows用法=============================================================================================

 

 

//toarray()把结果集转化为数组

$rowset = $table->fetchall ( "name != 'james'" ); //fetchrow()获取一行数据

//$row = $rowset->current();

//zend_debug::dump($row);

$rowset = $rowset->toarray (); //toarray()结果转化为关联数组

//zend_debug::dump($rowset,'',1);

 

//更新数据行

while ( $row = $table->fetchrow ( "name = 'chris'" ) ) { //使用while循环 批量修改符合条件数据

    $row->name = 'bevin'; //更新name字段

    $row->save (); //保存数据

}

 

//插入新数据行-------------------------------------------------------------------------

$rowset = $table->createrow ();

$rowset->id = null;

$rowset->name = 'chris';

$rowset->age = '53';

$rowset->save ();

 

//插入新数据 通过数组传值设置属性

$data = array ('id' => null, 'name' => 'tomson', 'age' => '32' );

$rowset = $table->createRow ( $data );

$rowset->save ();

 

//插入新数据通过设置fromarray

$data = array ('id' => null, 'name' => 'mary', 'age' => '34' );

$rowset = $table->createRow ();

$rowset->setfromarray ( $data );

$rowset->save ();

 

//删除数据行-----------------------------------------------------

while ( $rowset = $table->fetchRow ( "name = 'bevin'" ) ) {

    $rowset->delete ();

}


//序列化反序列化结果

$row = $table->fetchRow ( 'age = 32' );

$serialize = serialize ( $row );

echo $serialize;

$row = $row->toarray ();

zend_debug::dump ( $row );

 

//zend_db_rowset用法======================================================================================

$rowset = $table->fetchAll ( 'age = 12' );

$row = $rowset->current ();

zend_debug::dump ( $row, '', 1 );

 

$rowset = $table->fetchAll (); //查询table表 并更新部分字段数据

 

foreach ( $rowset as $val ) {

    $rand = rand ( 20, 100 );

    // echo get_class($val);

    $val->age = $rand;

    $val->save ();

}

?>

抱歉!评论已关闭.