成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁 ? 技術(shù)教程 ? Zend Framework數(shù)據(jù)庫(kù)操作技巧總結(jié)
全站頭部文字 我要出現(xiàn)在這里
Zend Framework數(shù)據(jù)庫(kù)操作技巧總結(jié) 719 2024-01-16   

本文實(shí)例總結(jié)了Zend Framework數(shù)據(jù)庫(kù)操作。分享給大家供大家參考,具體如下:

Zend_Db數(shù)據(jù)庫(kù)知識(shí)

例子:

Model文件:

$this->fetchAll("is_jian=1","id DESC",0,2)->toArray();
//根據(jù)is_jian=1,按id倒序排列取前2條記錄當(dāng)?shù)谝粋€(gè)參數(shù)為null時(shí),則直接按id倒序排列ASC為正序。

路由文件:

$video=new Video();//實(shí)例化數(shù)據(jù)庫(kù)類
$this->view->get2Video =$video->get2Video();//取到2條首頁推薦的數(shù)據(jù)

index.phtml文件:

<?php foreach ($this->get2Video as $video): ?>
<?=$video['id']; ?>
<?=$video['name']; ?>
<? endforeach; ?>

添加引號(hào)防止數(shù)據(jù)庫(kù)攻擊

quote用法

$value = $db->quote('St John"s Wort');
// $value 現(xiàn)在變成了 '"St John"s Wort"' (注意兩邊的引號(hào))
// 為數(shù)組加引號(hào)
$value = $db->quote(array('a', 'b', 'c'));
// $value 現(xiàn)在變成了 '"a", "b", "c"' (","分隔的字符串)

quoteInto用法

echo $where = $db->quoteInto('id = ?', 1);
// $where 現(xiàn)在為 'id = "1"' (注意兩邊的引號(hào))
// 在where語句中為數(shù)組加上引號(hào)
$where = $db->quoteInto('id IN(?)', array(1, 2, 3));
// $where 現(xiàn)在為 'id IN("1", "2", "3")' (一個(gè)逗號(hào)分隔的字符串)

(1)數(shù)據(jù)查詢總結(jié)

直接進(jìn)行查詢. ( 使用完整的sql語句)

//function quoteInto($text, $value, $type = null, $count = null)
$db = $this->getAdapter();
$sql = $db->quoteInto('SELECT * FROM `m_video` WHERE `is_guo` =?', '1');
$result = $db->query($sql);
// 使用PDOStatement對(duì)象$result將所有結(jié)果數(shù)據(jù)放到一個(gè)數(shù)組中
$videoArray = $result->fetchAll();

fetchAll用法

fetchAll($where = null, $order = null, $count = null, $offset = null)

取回結(jié)果集中所有字段的值,作為連續(xù)數(shù)組返回,如果參數(shù)不設(shè)置就寫成null

可以取回結(jié)果集的指定條數(shù)

$videoArray=$this->fetchAll("is_jian=1 and is_guo=1","id DESC",0,2)->toArray();

fetchAssoc用法

fetchAssoc($sql, $bind = array())

取回結(jié)果集中所有字段的值,作為關(guān)聯(lián)數(shù)組返回, 第一個(gè)字段作為碼

$db = $this->getAdapter();
$videoArray=$db->fetchAssoc("SELECT * FROM m_video WHERE `is_jian` = :title",array('title' => '1'));

fetchCol用法

fetchCol($sql, $bind = array())

取回所有結(jié)果行的第一個(gè)字段名

$db = $this->getAdapter();
$videoArray=$db->fetchCol("SELECT name FROM m_video WHERE `is_jian` = :title",array('title' => '1'));

fetchOne用法

fetchOne($sql, $bind = array())

只取回第一個(gè)字段值

$db = $this->getAdapter();
echo $videoArray=$db->fetchOne("SELECT count(*) FROM m_video WHERE `is_jian` = :title",array('title' => '1'));

fetchPairs用法

fetchPairs($sql, $bind = array())

取回一個(gè)相關(guān)數(shù)組,第一個(gè)字段值為碼(id),第二個(gè)字段為值(name)

返回:Array( [1] => 十二生肖奇緣 [2] => 桃花運(yùn)),1,2:為id字段。

$db = $this->getAdapter();
$videoArray=$db->fetchPairs("SELECT id, name FROM m_video WHERE is_jian = :title",array('title' => '1'));

fetchRow用法

fetchRow($where = null, $order = null)

只取回結(jié)果集的第一行

$videoArray=$this->fetchRow("is_jian=1 and is_guo=1", 'id DESC')->toArray();

query用法

//function query($sql, $bind = array())
$db = $this->getAdapter();
$result = $db->query('SELECT * FROM `m_video`');
//$result = $db->query('SELECT * FROM `m_video` WHERE `name` = ? AND id = ?',array('十二生肖奇緣', '1'));
//$result->setFetchMode(Zend_Db::FETCH_OBJ);//FETCH_OBJ為默認(rèn)值,FETCH_NUM,FETCH_BOTH
//while ($row = $result->fetch()) {
//  echo $row['name'];
//}
//$rows = $result->fetch();
//$rows = $result->fetchAll();
//$obj = $result->fetchObject();//echo $obj->name;
// echo $Column = $result->fetchColumn(0);//得到結(jié)果集的第一個(gè)字段,比如0為id號(hào),用于只取一個(gè)字段的情況
print_r($rows);

select用法

$db = $this->getAdapter();
$select = $db->select();
$select->from('m_video', array('id','name','clicks'))
->where('is_guo = :is_guo and name = :name')
->order('name')// 按什么排序列,參加為數(shù)組(多個(gè)字段)或字符串(一個(gè)字段)
->group()//分組
->having()//分組查詢數(shù)據(jù)的條件
->distinct()// 無參數(shù),去掉重復(fù)的值。有時(shí)候與groupby返回的結(jié)果一樣
->limit(10);
// 讀取結(jié)果使用綁定的參數(shù)
$params = array('is_guo' => '1','name'=>'十二生肖奇緣');
//$sql = $select->__toString();//得到查詢語句,可供調(diào)試
$result = $db->fetchAll($select,$params);
執(zhí)行select的查詢
$stmt = $db->query($select);
$result = $stmt->fetchAll();

或用

$stmt = $select->query();
$result = $stmt->fetchAll();

如果直接用

$db->fetchAll($select)

結(jié)果一樣

多表聯(lián)合查詢用法

$db = $this->getAdapter();
$select = $db->select();
$select->from('m_video', array('id','name','pic','actor','type_id','up_time'))
->where('is_guo = :is_guo and is_jian = :is_jian')
->order('up_time')
->limit(2);
$params = array('is_guo' => '1','is_jian'=>'1');
$select->join('m_type', 'm_video.type_id = m_type.t_id', 'type_name');//多表聯(lián)合查詢
$videoArray = $db->fetchAll($select,$params);

find()方法,可以使用主鍵值在表中檢索數(shù)據(jù).

// SELECT * FROM round_table WHERE id = "1"
$row = $table->find(1);
// SELECT * FROM round_table WHERE id IN("1", "2", 3")
$rowset = $table->find(array(1, 2, 3));

(2)數(shù)據(jù)刪除總結(jié)

第一種方法:可以刪任意表

//quoteInto($text, $value, $type = null, $count = null)
$table = 'm_video';// 設(shè)定需要?jiǎng)h除數(shù)據(jù)的表
$db = $this->getAdapter();
$where = $db->quoteInto('name = ?', 'ccc');// 刪除數(shù)據(jù)的where條件語句
echo $rows_affected = $db->delete($table, $where);// 刪除數(shù)據(jù)并得到影響的行數(shù)

第二種方法:只能刪除本表中的

//delete用法
// delete($where)
$where = "name = 'bbb'";
echo $this->delete($where);// 刪除數(shù)據(jù)并得到影響的行數(shù)

(3)數(shù)據(jù)更新總結(jié)

第一種方法:可以更新任意表

// 以"列名"=>"數(shù)據(jù)"的格式構(gòu)造更新數(shù)組,更新數(shù)據(jù)行
$table = 'm_video';// 更新的數(shù)據(jù)表
$db = $this->getAdapter();
$set = array (
'name' => '蝶影重重',
'clicks' => '888',
);
$where = $db->quoteInto('id = ?', '10');// where語句
// 更新表數(shù)據(jù),返回更新的行數(shù)
echo $rows_affected = $db->update($table, $set, $where);

第二種方法:只能更新本表中的

$set = array (
'name' => '蝶影重重22',
'clicks' => '8880',
);
$db = $this->getAdapter();
$where = $db->quoteInto('id = ?', '10');// where語句
$rows_affected = $this->update($set, $where);// 更新表數(shù)據(jù),返回更新的行數(shù)

(4)數(shù)據(jù)插入總結(jié)

第一種方法:可以在任意表中插入數(shù)據(jù)

$table = 'm_gao';// 插入數(shù)據(jù)的數(shù)據(jù)表
$db = $this->getAdapter();
// 以"列名"=>"數(shù)據(jù)"的格式格式構(gòu)造插入數(shù)組,插入數(shù)據(jù)行
$row = array (
'title'   => '大家好。111',
'content' => '影視網(wǎng)要改成用zend framework開發(fā)啊',
'time' => '2009-05-04 17:23:36',
);
// 插入數(shù)據(jù)行并返回插入的行數(shù)
$rows_affected = $db->insert($table, $row);
// 最后插入的數(shù)據(jù)id
echo $last_insert_id = $db->lastInsertId();
$row=array(
'name'=>'curdate()',
'address' => new Zend_Db_Expr ('curdate()')
)

這樣子字段name會(huì)插入一個(gè)curdate()的字符串,而address插入一個(gè)時(shí)間值(curdate()的結(jié)果2009-05-09)

第二種方法:只能適合本表中的還沒有總結(jié)出來

(5)事務(wù)處理

$table = 'm_gao';// 插入數(shù)據(jù)的數(shù)據(jù)表
$db = $this->getAdapter();
$db->beginTransaction();//Zend_Db_Adapter會(huì)回到自動(dòng)commit模式下,直到你再次調(diào)用 beginTransaction()方法
// 以"列名"=>"數(shù)據(jù)"的格式格式構(gòu)造插入數(shù)組,插入數(shù)據(jù)行
$row = array (
'id'=>null,
'title'   => '大家好。111',
'content' => '影視網(wǎng)要改成用zend framework開發(fā)啊',
'time' => '2009-05-04 17:23:36',
);
try {
// 插入數(shù)據(jù)行并返回插入的行數(shù)
$rows_affected = $db->insert($table, $row);
// 最后插入的數(shù)據(jù)id
$last_insert_id = $db->lastInsertId();
$db->commit();// 事務(wù)提交
}catch (Exception $e){
$db->rollBack();
echo '捕獲異常:'.$e->getMessage();//打出異常信息
}
echo $last_insert_id;

(6)其他

$db = $this->getAdapter();
$tables = $db->listTables(); //列出當(dāng)前數(shù)據(jù)庫(kù)中的所有表
$fields = $db->describeTable('m_video');//列出一個(gè)表的字段情況

更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。



主站蜘蛛池模板: 提升自我| 太卷了正确答案| 小班健康活动教案40篇| 诗第十二主要内容| 小女孩屁股| 网页抖音| 李采潭和闵度允| 雪豹46集全| 电影《19号海滩》在线播放国语版| 美女视频网站黄色| 做生活的高手| 美姐妹| 唐朝诡事录在线观看全集免费观看| 装饰色彩| 赵在允| 纸牌屋电影| 李轻扬| 我的新学校英语作文| 男同性恋免费视频| 吴承轩主演的电视剧| 恶魔 电影| 韩绛| 恋爱三万英尺| 甲种公牛1976| 泰国av| 电影《在云端》| 搜狐视频官网| 山东生活频道| 李正夏| 十大名茶排名顺序| 佳片有约| 我在碧桂园的1000天| 眼睛胬肉手术多久恢复| 甲铁城的卡巴内瑞海门决战| 舌吻床戏视频| 蓝家宝电影| 夜半2点钟| 好一个中国大舞台简谱| 池田夏希| 复仇之路| 帕兰妮·琳帕缇雅空|

!!!站長(zhǎng)長(zhǎng)期在線接!!!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長(zhǎng)微信:lxwl520520

站長(zhǎng)QQ:1737366103