2010年2月4日 星期四

ZendFramework 代碼片段

config.ini 文件
[general]
db.adapter = PDO_MYSQL
db.config.host = localhost
db.config.username = root
db.config.password =
db.config.dbname = zftest

$config=new Zend_Config_Ini('./config/config.ini','general');
//zend_config_ini加載配置文件用
$db=Zend_Db::factory($config->db->adapter,
$config->db->config->toArray());
Zend_Db::factory("資料庫的類型","把config文件中的内容换成陣列")

防止資料庫攻擊方法:
1、$value=$db->quote('ADFA'DF');
quote自動為單引號行過濾加上雙引号
2、quoteInto()
$value=$db->quoteInto();
多筆查詢


$db->quoteInto('(a =? AND ', $a) .
$db->quoteInfo('b = >)', $b) . 
$db->quoteInfo(' OR ( c != ?)', $c)

3、直接查询
$sql=$db->quoteInto('select * from example where date>?','2006-01-01');
$result=$db->query($sql);
$rows=$result->fetchAll();
4、或者使用占位符号:placeholder 如:
$result=$db->query('select * from exaple where date>:placeholder',
array('placeholder'=>'2006-01-01')
$rows=$result->fetchAll();
5、prepare()方法绑定
$stmt=$db->prepare('select * from example where date>:placeholder');
$stmt->bindvalue('placeholder','2006-01-01');
$stmt->execute();
$rows=$stmt->fetchAll();
6、交易處理
$db->beginTransaction();
try{
$db->query(...);
$db->commit();
}catch(Exception $e){
$db->rollback();
echo $d->getMessage();
}
7、插入數據行
$row=array('title'=>'king','name'=>'baobao',color=>'blue');
$table='uer_table';
$rows_affected=$db->insert($table,$row);
$last_insert_id=$db->lastInsertId();
8、更新數據行
$set=array('name'=>'lailai')
$table='user_table'//更新的数据表
//where語法
$where=$db->quotuinto('name=?','baobao');
//更新資料表,回傳行數
$rows_affected=$db->update($table,$set,$where);
9、刪除資料航
$table='user_table'
where 語句
$where =$db->quoteinto('first_name=?','patsy');
//删除資料並得到影響的行數
$rows_affected=$db->delete($table,$where);
10、取回查詢結果
fetchAll();fetchAssoc();fetchCol();fetchOne();fetchPairs();fetchRow();
//fetchAll取回所有結果集合,並作為連續的雜湊返回
$result=$db->fetchAll(
'select * from round_table where noble_title=:title',
array('title'=>'sir')
);
fetchAssoc()//作为关联数组返回
fetchcol()//取回结果行的第一个字段名
fetchOne()//取回第一个字段值
fetchPairs()//取回一个相关数组,第一个字段为码,第二个字段为值
fetchRow()//取得结果集中的第一行
$config = new Zend_Config_Ini(APP_DIR . '/config/config.ini', 'general');

$this->getHelper('layout')->disableLayout();
$this->getHelper('viewRenderer')->setNoRender();

headScript()->captureStart(); ?>
headScript()->captureEnd(); ?>


$this->view->getHelper('userMemUrl')->userMemUrl('info')

// 分頁頁碼
$this->per = 10;
$len = $this->db->fetchOne("select count(*) from `feedback` where `active`=1 and `memorial_id`=$this->mid");
$pager = new Pager($len, $this->per);
$pager->setAlign('right');
$this->view->pager = $pager->getNavigation();
$start = ($pager->getCurrentPage()-1)*$this->per;
$obj = new Feedback();
$this->view->list = $obj->fetchAll("`active`=1", 'poscode desc', $this->per, $start);


/* 读取上一张,下一张 */
$this->view->prevRow = $obj->fetchRow("`active`=1 and `id`='$row->id' and `poscode`>'$row->poscode'", 'poscode asc', 1, 0);
$this->view->nextRow = $obj->fetchRow("`active`=1 and `id`='$row->id' and `poscode`<'$row->poscode'", 'poscode desc', 1, 0);

上一条:
prevRow): ?>
prevRow->title; ?>


这是第一条


下一条:
nextRow): ?>
nextRow->title; ?>

这是最后一条



$this->view->headLink()->appendStylesheet($this->baseDir . '/css/gardens.css');

$this->view->headScript()->appendFile($this->baseDir . '/js/jquery.js');
$this->view->headScript()->appendFile($this->baseDir . '/js/common.js');


if(!$this->_request->isPost()) {
$this->view->useIframe = true;
} else {
$data = $this->_request->getPost();
if(!$data['content']) {
echo Func::feedtop('alert("生平簡介不能為空,請重新輸入!");');
exit();
}
$row->content = $data['content'];
$rs = $row->save();
if($rs) {
echo Func::feedtop('alert("生平簡介更新成功!");');
exit();
} else {
echo Func::feedtop('alert("生平簡介更新失敗,請稍後重試!");');
exit();
}
}

// ajax 響應
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('select-wish', 'html')
->addActionContext('send-wish', 'html')
->initContext();


$this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');

$this->_flashMessenger->addMessage('修改成功!');
$this->view->messages = $this->_flashMessenger->getMessages();


echo Func::feedtop('winFunc.go("' .$this->view->getHelper('baseUrl')->baseUrl() . '/user/pay/cart/id/' .$rs. '");');



// 刷新 SESSION
$auth = $this->getInvokeArg('auth');
$auth->getStorage()->write($row);


//驗證碼
$this->vcode = new Zend_Session_Namespace('vcode');
if ($data['vcode'] != $this->vcode->user_login) {
echo Func::feedtop('alert("驗證碼輸入有誤,請確認!");$("#vcode")[0].focus()');
exit();
}

沒有留言: