Zend_Db數據庫知識
例子:
Model文件:
$this->fetchAll("is_jian=1","id DESC",0,2)->toArray();//根據is_jian=1,按id倒序排列取前2條記錄當第一個參數為null時,則直接按id倒序排列ASC為正序。
路由文件:
$video=new Video();//實例化數據庫類
$this->view->get2Video =$video->get2Video();//取到2條首頁推薦的數據
index.phtml文件:
<?php foreach ($this->get2Video as $video): ?>
<?=$video['id']; ?>
<?=$video['name']; ?>
<? endforeach; ?>
添加引號防止數據庫攻擊
quote用法
$value = $db->quote('St John"s Wort');
// $value 現在變成了 '"St John/"s Wort"' (注意兩邊的引號)
// 為數組加引號
$value = $db->quote(array('a', 'b', 'c'));
// $value 現在變成了 '"a", "b", "c"' (","分隔的字符串)
quoteInto用法
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語句)
//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
可以取回結果集的指定條數
$videoArray=$this->fetchAll("is_jian=1 and is_guo=1","id DESC",0,2)->toArray();
fetchAssoc用法
fetchAssoc($sql, $bind = array())
取回結果集中所有字段的值,作為關聯數組返回, 第一個字段作為碼
$db = $this->getAdapter();
$videoArray=$db->fetchAssoc("SELECT * FROM m_video WHERE `is_jian` = :title",array('title' => '1'));
fetchCol用法
fetchCol($sql, $bind = array())
取回所有結果行的第一個字段名
$db = $this->getAdapter();
$videoArray=$db->fetchCol("SELECT name FROM m_video WHERE `is_jian` = :title",array('title' => '1'));
fetchOne用法
fetchOne($sql, $bind = array())
只取回第一個字段值
$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字段。
$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)
只取回結果集的第一行
$videoArray=$this->fetchRow("is_jian=1 and is_guo=1", 'id DESC')->toArray();
query用法
//function query($sq
新聞熱點
疑難解答