zend framework连接mysql
<?php
set_include_path('D:\apache\htdocs\zend\\');//zend 路径
require_once 'zend/db.php';
//数据库参数配置
$params = array(
'host'=>'127.0.0.1',
'username'=>'root',
'password'=>'root',
'dbname'=>'test1'
);
$db = Zend_Db::factory('pdo_mysql',$params);
//if($db){
// echo 'ok';
//}
//=========================================================================================================
$tbl = 'tbl1';
$select = $db->select();
$select->from('tbl1','*');
$where = $db->quoteInto("age<(?)",30);
$select->where($where);
$select->where($db->quoteInto('age in (?)',array('23','25')));
$rows = $db->fetchAll($select);
foreach ($rows as $key=>$val){
echo 'name:'.$rows[$key][name].' age:'.$rows[$key][age];
echo '<br />';
}
$value = $db->quote('my name is "tom"');
echo $value;
$value = $db->quote(array('a','b','c'));
require 'zend/debug.php';
zend_debug::dump($rows,'zend_dubut::dump : <br />',1);
//echo $where;
$sql = $db->query('select * from tbl1 where age>(?)','30');
$rows = $sql->fetchall();
zend_debug::dump($rows);
//使用占位符
$sql = $db->query('select * from tbl1 where age>:zw and age<:zw2 ',array('zw'=>23,'zw2'=>40));
$rows = $sql->fetchall();
zend_debug::dump($rows);
//pdostatement 方法
$stm = $db->prepare('select * from tbl1 where age>:zw');
$stm->bindvalue('zw','23');
$stm->execute();
$rows = $stm->fetchall();
zend_debug::dump($rows);
echo "<hr />";
//事务处理
$db->beginTransaction();// 尝试数据库操作.// 假如成功,commit该操作;// 假如,
try {
$db->query("select * from tbl1 where age>234");
$db->commit();
}
catch (Exception $e) {
$db->rollBack();
echo $e->getMessage();
}
//插入数据
$rand = rand(59,100);
$rows = array('name'=>'liming','age'=>$rand);
$tblname = 'tbl1';
$db->insert($tblname,$rows);
$lastinsertid = $db->lastInsertId();
if ($db)
echo $lastinsertid;
echo "<hr />";
//更新数据
$tblname ='tbl1';
$set = array('age'=>222);
$where = $db->quoteInto('name=?','liming');
$db->update($tblname,$set,$where);
//删除数据
$where = $db->quoteInto('name=?','liming');
$db->delete($tblname,$where);
//几种不同取得结果方法
$sql = $db->quoteInto('select * from tbl1 where age>?',23);
$rows = $db->fetchAll($sql);
zend_debug::dump($rows,'fecthall方法(取回所有结果集以连续数组显示):',1);//取回所有结果集以连续数组显示
$rows = $db->fetchAssoc($sql);
zend_debug::dump($rows,'fetchassoc方法(取回所有结果集以关联数组显示):',1);
$rows = $db->fetchCol($sql);//取回所有0
zend_debug::dump($rows,'fetchcol方法(取回所有结果行的第一个字段值):',1);
$rows = $db->fetchOne($sql);
zend_debug::dump($rows,'fetchone方法(只取回第一个字段值):',1);
$rows = $db->fetchRow($sql);
zend_debug::dump($rows,'fetchrow方法(取回结果集第一行)',1);
$rows = $db->fetchPairs($sql);
zend_debug::dump($rows,'fetchpairs方法(取回一对结果集 第一个一)');
//=====================================================================================================