user5534204
user5534204

Reputation:

Yii queryScalar() with mysql queries?

I want to find the user role for the matching id and email of logged user. below is my wbuser class

I'm getting following error

Error 500
CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

here is my code

<?php

class WebUser extends CWebUser {
    public $role;
    public $role2;

    public function getRole2() {
        if ($this->role2 === null) {
            // Here you need to get User role
            $this->role2 = Yii::app()->db->createCommand("SELECT email FROM {{students}} WHERE id=:id AND role=1")->queryScalar(array(':id' => Yii::app()->user->id));
        }

        return $this->role2;
    }
    public function getRole(){
        if ($this->role === null) {
            // Here you need to get User role
            $this->role = Yii::app()->db->createCommand("SELECT role FROM {{students}} WHERE id=:id AND email=':email'")->queryScalar(array(':id' => Yii::app()->user->id,':email' => $this->role2,));
       } 
        return $this->role;
    }
}

Upvotes: 1

Views: 3695

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133380

Try this way

$this->role2 = Yii::app()->db->createCommand()
->select('email')
->from( '{{students}}' )
->where('id=:id AND role=1', array(':id'=>$id));
->queryScalar();

and for the second query try

$this->role2 = Yii::app()->db->createCommand()
->select('role')
->from( '{{students}}')
->where('id=:id AND email=:email', array(':id'=>$id, ':email'=> $this->role2));
->queryScalar();

Upvotes: 2

Related Questions