サンプル1
http://blogs.yahoo.co.jp/dk521123/24302172.htmlのデータを使う。
<?php try { require_once 'Zend/Db.php'; require_once 'Zend/Config/Ini.php'; $config = new Zend_Config_Ini('./configs/db_info.ini', 'hello_db'); $db = Zend_Db::factory($config->db); $select = $db->select(); $select->from('message_board', '*'); $rows = $db->fetchAll($select); // 文字化け対策1 mb_language("Japanese"); mb_internal_encoding("UTF8"); mb_detect_order("ASCII, UTF-8, JIS, EUC-JP, SJIS"); foreach ($rows as $row) { $id = $row["id"]; $message = mb_convert_encoding($row["message"], "UTF8", "auto"); echo "id : " . $id . "<br>"; echo "message : " . $message . "<br>"; } } catch (Zend_Exception $e) { echo $e; exit(); } ?>
構文
where / order
$select = $db->select(); $select->from('tableName', '*'); $select->where('isOpened = ?', true); $select->order('date DESC'); $select->order('id'); $rows = $db->fetchAll($select);
Max
$select = $db->select(); $select->from('tableName', 'MAX(id)'); $maxId = $db->fetchOne($select);http://zend-framework.developpez.com/faq/?page=db-select
COUNT
$select = $db->select(); $select->from('tableName', 'COUNT(*) AS count'); $select->where('artId = ?', $id); $count = $db->fetchOne($select);
別方法1
以下でも可能!(これがいい)http://framework.zend.com/manual/en/zend.db.table.rowset.html
$select = $db->select(); $select->from('tableName', '*'); $select->where('isOpened = ?', true); $select->order('date DESC'); $select->order('id'); $rows = $db->fetchAll($select); $count = count($rows);
別方法2
以下でも可能?http://www.zfforums.com/zend-framework-components-13/databases-20/i-want-count-rows-537.html
... $select->from('tableName', 'COUNT(*) AS num'); $count = $db->fetchRow($select)->num;
join
$select = $db->select(); $select->from("tableName1"); $select->join("tableName2", "tableName1id= tableName2.id", "*"); $select->order('tableName1.date DESC'); $rows = $db->fetchAll($select);
参考文献
http://pluto-blog.blog.so-net.ne.jp/2007-01-15-1http://pentan.info/php/zend_fw/zend_db_select.html
limit
limit(【取得する件数】, 【オフセット(開始位置)】); ※ オフセット(開始位置)は0発進
サンプル
$select = $db->select(); $select->from("tableName1"); $select->join("tableName2", "tableName1id= tableName2.id", "*"); $select->limit(10, 25); $select->order('tableName1.date DESC'); $rows = $db->fetchAll($select);