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ù)類(lèi)
$this->view->get2Video =$video->get2Video();//取到2條首頁(yè)推薦的數(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語(yǔ)句中為數(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ù)查詢(xún)總結(jié)
直接進(jìn)行查詢(xún).( 使用完整的sql語(yǔ)句)
//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è)置就寫(xiě)成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($sq
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注