查询有语法错误? [ 新手入门 ]
public function getPost()
{
$criteria= new CDbCriteria;
$criteria->select='name,create_time';
$criteria->join='posts';
$criteria->limit=30;
$criteria->order='create_time DESC';
$criteria->condition='user_id=:user_id';
$criteria->params=array(':user_id'=> Yii::app()->user->id);
$post= Exercise::model()->findAll($criteria);
return $post;
}
为什么运行了就报错呢,语法有问题吗
另外$criteria->condition='user_id=:user_id';
可以改成$criteria->condition=array('user_id'=>Yii::app()->user->id);
这样可以么
共 5 条回复
-
$criteria= new CDbCriteria; $criteria->select='name,create_time'; $criteria->join='posts'; $criteria->limit=30; $criteria->order='create_time DESC'; $criteria->compare('user_id', Yii::app()->user->id); $post= Posts::model()->findAll($criteria); return $post;
CDbCommand
CDbCommand 无法执行 SQL 语句:SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'posts WHERE user_id='1' ORDER BY create_time DESC LIMIT 30' at line 1. The SQL statement executed was: SELECT name,create_time FROM `posts` `t` posts WHERE user_id=:ycp0 ORDER BY create_time DESC LIMIT 30 controller $post= Posts::model()->getNewRecord(); $this->render('index',array( 'post'=>$post, ) views <?php echo $post; ?>
evoself
注册时间:2011-03-01
最后登录:2013-08-02
在线时长:0小时0分
最后登录:2013-08-02
在线时长:0小时0分
- 粉丝1
- 金钱420
- 威望0
- 积分420