lstrjstr
(Manstersm)
September 13, 2011, 12:27pm
1
Good Day,
I have this problem in how to get each data result and pass it into my model attributes. my query is like this.
$command = Yii::app()->db->createCommand();
$query = $command->select('*')->from('tbl_user')->where('id=:id',array(':id'=>$id))->queryAll();
i think bindColumn is the one i need but i cant get exactly how to code it. is there a way like
(1st column result) = $this->var1;
hope someone can help
Thanks!
Haensel
(Johannes)
September 13, 2011, 12:38pm
2
lstrjstr
(Manstersm)
September 13, 2011, 12:42pm
3
can you post the code here?
or or how can i set the :id to my variable $id?
$sql="SELECT username, email FROM tbl_user WHERE id=:id";
Haensel
(Johannes)
September 13, 2011, 1:02pm
4
Ok, I am a bit confused now. Do you want to bind a variable to your "where" statement (like in your last post) or do you want to bind the columns of your result to variables like you said in your first post? There is a difference
$command->bindParam(":id",$id,PDO::PARAM_STR);
will replace :id with the value of $id
if you want to bind the resulting columns to variables you would use something like that.
$response=$command->query()->bindColumn(1,$username);
while($response->read()!==false)
{
// $username contains the username of the current row
}
mbi
(mbi)
September 13, 2011, 2:28pm
5
lstrjstr:
I have this problem in how to get each data result and pass it into my model attributes. my query is like this.
$command = Yii::app()->db->createCommand();
$query = $command->select('*')->from('tbl_user')->where('id=:id',array(':id'=>$id))->queryAll();
do you mean http://www.yiiframework.com/doc/api/1.1/CActiveRecord#populateRecord-detail
lstrjstr
(Manstersm)
September 14, 2011, 11:44pm
6
Haensel:
Ok, I am a bit confused now. Do you want to bind a variable to your "where" statement (like in your last post) or do you want to bind the columns of your result to variables like you said in your first post? There is a difference
$command->bindParam(":id",$id,PDO::PARAM_STR);
will replace :id with the value of $id
if you want to bind the resulting columns to variables you would use something like that.
$response=$command->query()->bindColumn(1,$username);
while($response->read()!==false)
{
// $username contains the username of the current row
}
haha sorry for the inconsistency of my posts. but thanks! you answered both of my questions!!
Cheers!