curd——5

SELECT area_id  FROM 16tree.ts_area  where pid=0;


<?php
//1可以防止注入
$db = Yii::app()->db; //you have to define db connection in config/main.php
$sql = "select sum(if(starttime>'09:00:00',1,0)) as late,sum(if(endtime<'18:00:00',1,0)) as early from present where userid=:userid and date between :date_start and :date_end";
$results = $db->createCommand($sql)->query(array(':userid' => 115,':date_start'=>'2009-12-1',':date_end'=>'2009-12-31',));
foreach($results as $result){
  echo $result['late']," and ",$result['early']," /n";
}





//2可以防止注入

$oDbConnection = Yii::app()->db; // 获取数据库连接(config / main.php设置链接的数据库
// 在这里,你将用你的复杂的SQL查询使用字符串或其他一的方式来创建查询
$oCommand = $oDbConnection->createCommand('SELECT * FROM my_table WHERE myAttr = :myValue');
// 绑定参数
$oCommand->bindParam(':myValue', $myValueThatCameFromPostOrAnywereElse, PDO::PARAM_STR);
 

$oCDbDataReader = $oCommand->queryAll(); // 运行查询并在cdbdatareader获得所有的结果
?>

原文地址:https://www.cnblogs.com/ldms/p/8317869.html