激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - Zend Framework數據庫操作技巧總結

Zend Framework數據庫操作技巧總結

2021-04-24 17:20小炒花生米 PHP教程

這篇文章主要介紹了Zend Framework數據庫操作技巧,結合實例形式總結分析了Zend Framework針對數據庫操作的常見函數、常用操作及相關注意事項,需要的朋友可以參考下

本文實例總結了Zend Framework數據庫操作。分享給大家供大家參考,具體如下:

Zend_Db數據庫知識

例子:

Model文件:

?
1
2
$this->fetchAll("is_jian=1","id DESC",0,2)->toArray();
//根據is_jian=1,按id倒序排列取前2條記錄當第一個參數為null時,則直接按id倒序排列ASC為正序。

路由文件:

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

index.phtml文件:

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

添加引號防止數據庫攻擊

quote用法

?
1
2
3
4
5
$value = $db->quote('St John"s Wort');
// $value 現在變成了 '"St John\"s Wort"' (注意兩邊的引號)
// 為數組加引號
$value = $db->quote(array('a', 'b', 'c'));
// $value 現在變成了 '"a", "b", "c"' (","分隔的字符串)

quoteInto用法

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

(1)數據查詢總結

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

?
1
2
3
4
5
6
//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對象$result將所有結果數據放到一個數組中
$videoArray = $result->fetchAll();

fetchAll用法

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

取回結果集中所有字段的值,作為連續數組返回,如果參數不設置就寫成null

可以取回結果集的指定條數

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

fetchAssoc用法

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

取回結果集中所有字段的值,作為關聯數組返回, 第一個字段作為碼

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

fetchCol用法

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

取回所有結果行的第一個字段名

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

fetchOne用法

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

只取回第一個字段值

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

fetchPairs用法

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

取回一個相關數組,第一個字段值為碼(id),第二個字段為值(name)

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

?
1
2
$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)

只取回結果集的第一行

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

query用法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//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為默認值,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);//得到結果集的第一個字段,比如0為id號,用于只取一個字段的情況
print_r($rows);

select用法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$db = $this->getAdapter();
$select = $db->select();
$select->from('m_video', array('id','name','clicks'))
->where('is_guo = :is_guo and name = :name')
->order('name')// 按什么排序列,參加為數組(多個字段)或字符串(一個字段)
->group()//分組
->having()//分組查詢數據的條件
->distinct()// 無參數,去掉重復的值。有時候與groupby返回的結果一樣
->limit(10);
// 讀取結果使用綁定的參數
$params = array('is_guo' => '1','name'=>'十二生肖奇緣');
//$sql = $select->__toString();//得到查詢語句,可供調試
$result = $db->fetchAll($select,$params);
執行select的查詢
$stmt = $db->query($select);
$result = $stmt->fetchAll();

或用

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

如果直接用

?
1
$db->fetchAll($select)

結果一樣

多表聯合查詢用法

?
1
2
3
4
5
6
7
8
9
$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');//多表聯合查詢
$videoArray = $db->fetchAll($select,$params);

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

?
1
2
3
4
// 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)數據刪除總結

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

?
1
2
3
4
5
//quoteInto($text, $value, $type = null, $count = null)
$table = 'm_video';// 設定需要刪除數據的表
$db = $this->getAdapter();
$where = $db->quoteInto('name = ?', 'ccc');// 刪除數據的where條件語句
echo $rows_affected = $db->delete($table, $where);// 刪除數據并得到影響的行數

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

?
1
2
3
4
//delete用法
// delete($where)
$where = "name = 'bbb'";
echo $this->delete($where);// 刪除數據并得到影響的行數

(3)數據更新總結

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

?
1
2
3
4
5
6
7
8
9
10
// 以"列名"=>"數據"的格式構造更新數組,更新數據行
$table = 'm_video';// 更新的數據表
$db = $this->getAdapter();
$set = array (
'name' => '蝶影重重',
'clicks' => '888',
);
$where = $db->quoteInto('id = ?', '10');// where語句
// 更新表數據,返回更新的行數
echo $rows_affected = $db->update($table, $set, $where);

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

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

(4)數據插入總結

第一種方法:可以在任意表中插入數據

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$table = 'm_gao';// 插入數據的數據表
$db = $this->getAdapter();
// 以"列名"=>"數據"的格式格式構造插入數組,插入數據行
$row = array (
'title'   => '大家好。111',
'content' => '影視網要改成用zend framework開發啊',
'time' => '2009-05-04 17:23:36',
);
// 插入數據行并返回插入的行數
$rows_affected = $db->insert($table, $row);
// 最后插入的數據id
echo $last_insert_id = $db->lastInsertId();
$row=array(
'name'=>'curdate()',
'address' => new Zend_Db_Expr ('curdate()')
)

這樣子字段name會插入一個curdate()的字符串,而address插入一個時間值(curdate()的結果2009-05-09)

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

(5)事務處理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$table = 'm_gao';// 插入數據的數據表
$db = $this->getAdapter();
$db->beginTransaction();//Zend_Db_Adapter會回到自動commit模式下,直到你再次調用 beginTransaction()方法
// 以"列名"=>"數據"的格式格式構造插入數組,插入數據行
$row = array (
'id'=>null,
'title'   => '大家好。111',
'content' => '影視網要改成用zend framework開發啊',
'time' => '2009-05-04 17:23:36',
);
try {
// 插入數據行并返回插入的行數
$rows_affected = $db->insert($table, $row);
// 最后插入的數據id
$last_insert_id = $db->lastInsertId();
$db->commit();// 事務提交
}catch (Exception $e){
$db->rollBack();
echo '捕獲異常:'.$e->getMessage();//打出異常信息
}
echo $last_insert_id;

(6)其他

?
1
2
3
$db = $this->getAdapter();
$tables = $db->listTables(); //列出當前數據庫中的所有表
$fields = $db->describeTable('m_video');//列出一個表的字段情況

希望本文所述對大家基于Zend Framework框架的PHP程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美国一级黄色毛片 | 中文字幕 欧美 日韩 | 欧美韩国日本在线 | 日韩精品中文字幕一区二区三区 | 手机国产乱子伦精品视频 | 羞羞答答xxdd在线播放 | 国产精品免费在线 | 国产视频第一区 | 亚洲人成网站免费播放 | lutube成人福利在线观看 | 91精品国产91久久久久久吃药 | 国产亚洲精品综合一区91 | 色日本视频 | 91九色蝌蚪国产 | 成人永久免费视频 | 成人性视频欧美一区二区三区 | 国产午夜精品视频免费不卡69堂 | 亚洲第五色综合网 | 欧美a视频在线观看 | 中文字幕爱爱视频 | 国产精品视频一区二区噜噜 | 牛牛热这里只有精品 | 欧美一级久久 | 免费在线观看毛片视频 | 久久色伦理资源站 | 337p粉嫩大胆噜噜噜亚瑟影院 | 成人午夜精品久久久久久久蜜臀 | 久久国产一二三 | 国产91久久久久久 | 激情综合在线观看 | 女人一区二区三区 | 黄色网址入口 | 色欲香天天天综合网站 | 日本成人一二三区 | 国产91小视频在线观看 | 久久久久久久久久久av | xxxx18韩国护士hd老师 | 欧洲色阁中文字幕 | 男人午夜小视频 | 天天都色视频 | 92看片淫黄大片欧美看国产片 |