YII 框架 第十天(5)數(shù)據(jù)處理之DAO
Yii對(duì)數(shù)據(jù)的處理有 AR(之前使用的就是)特點(diǎn)就是簡(jiǎn)單的sql處理
還有 DAO 處理復(fù)雜sql語(yǔ)句
兩者使用的都是PDO
查詢 query
非查詢 execute
1
????????public?function?actionDaoTest() ????????{ ????????????//查詢數(shù)據(jù) ???????????? ????????????$sql?=?'select?*?from?{{user}}'; ????????????$dao?=?Yii::app()->db->createCommand($sql); ????????????var_dump($dao->queryRow()); ????????????????????//queryAll ????????????????????//queryColumn ????????}
2
????????public?function?actionDaoTest() ????????{ ????????????//添加數(shù)據(jù) ???????????? ????????????$sql?=?"insert?into?{{user}}(username,password)?values('rabbit','turnip')"; ????????????$dao?=?Yii::app()->db->createCommand($sql); ????????????echo?$dao->execute(); ????????}
3
使用占位符,有利于安全
????????public?function?actionDaoTest() ????????{ ????????????//安全添加數(shù)據(jù) ???????????? ????????????$sql?=?"insert?into?{{user}}(username,password)?values(:name,:pwd)"; ????????????$name?=?'lion'; ????????????$pwd?=?'meat'; ????????????$dao?=?Yii::app()->db->createCommand($sql); ????????????$dao?->?bindParam(':name',$name,PDO::PARAM_STR); ????????????$dao?->?bindParam(':pwd',$pwd,PDO::PARAM_STR);???????????? ????????????echo?$dao->execute(); ????????}