转换为Yii查询生成器

时间:2013-11-21 22:00:36

标签: php mysql yii

我想将其转换为查询构建器表达式。 我尝试了很多,但我不能这样做。如果可以,请帮帮我

$connect = mysql_connect("localhost","root","") or die("not connecting");
    mysql_select_db("skykeey",$connect) or die("no db :'(");

    $find1 =mysql_query("SELECT Count(*) FROM `mosqueculturalliablee` WHERE `email` ='$this->username'",$connect);
    $count1=mysql_fetch_row($find1);

我将自己转换为:

$find1 =Yii::app()->db->createCommand()
    ->select ('count(*) as num')
    ->from('mosqueculturalliablee')
    ->where('email=' . $this->username)
    ->queryScalar();

但它不起作用,我得到mysql语法错误

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您尚未在查询中包含$this->username的引号。

 ->where("email='" . $this->username."'") //outputs where email='username'

作为改进,您应该使用PDO代替。

->where("email=:email")
->queryScalar(array(':email'=>$this->username));