Dear all,
I’m having a problem binding a string value to the sql. I have to bind lang value to the sql, but it doesn’t seem to fit in as I don’t get the required result. I need to have a query like ... AND lang='en'
but when I bind the string to the sql, I have written '... AND lang=:lang', array(':lang'=>Yii::app()->language)
. However, it doesn’t work. How can I solve the problem?
Any help is highly appreciated.
Best Regards
vibhaj
(Vibha Jadwani)
July 26, 2012, 11:51am
2
post your code where you have added language filteration.
jayant
(Codesutras)
July 26, 2012, 12:05pm
3
elitsolutions:
Dear all,
I’m having a problem binding a string value to the sql. I have to bind lang value to the sql, but it doesn’t seem to fit in as I don’t get the required result. I need to have a query like ... AND lang='en'
but when I bind the string to the sql, I have written '... AND lang=:lang', array(':lang'=>Yii::app()->language)
. However, it doesn’t work. How can I solve the problem?
Any help is highly appreciated.
Best Regards
Please share your code to get a appropriate result.
Sorry, for not posting the code which caused confusion. Below is the code:
$results->select('pl.id, pl.l_title')
->from('post p1')
->leftjoin('postLang pl', 'pl.post_id=p1.id AND pl.lang_id=:lang', array(':lang'=>(string)Yii::app()->language))
->where('p1.parent_id = :mid', array(':mid'=>(int)$menu_id));
I get result set but with empty values which is, I guess, because of string binding.
Best Regards
jayant
(Codesutras)
July 26, 2012, 12:39pm
5
elitsolutions:
Sorry, for not posting the code which caused confusion. Below is the code:
$results->select('pl.id, pl.l_title')
->from('post p1')
->leftjoin('postLang pl', 'pl.post_id=p1.id AND pl.lang_id=:lang', array(':lang'=>(string)Yii::app()->language))
->where('p1.parent_id = :mid', array(':mid'=>(int)$menu_id));
I get result set but with empty values which is, I guess, because of string binding.
Best Regards
Ok if you thik that it is due to string binding problem then try to bind data something like this.
$lang=(string)Yii::app()->language;
array(':lang'=>"'.$lang.'");
lets see what happen
abennouna
(Abennouna)
July 26, 2012, 12:41pm
6
Hello,
I don’t think you need to cast Yii::app()->language to a string, it already is.
Anyway, I believe you have syntax errors, your code should be
$results->select('pl.id, pl.l_title')
->from('post p1')
->leftjoin('postLang pl', 'pl.post_id=p1.id AND pl.lang_id = ":lang"', array(':lang'=>Yii::app()->language))
->where('p1.parent_id = :mid', array(':mid'=>(int)$menu_id));
PS What are the odds you chose p1 and pl as alias names, very similar in a monotype font
Edit: Jayant answered first
Thank you, guys.
bennouna, I have tried your version but I got the below error:
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens. The SQL statement executed was: SELECT `p1`.`id`, `p1`.`title`
FROM `post` `p1`
LEFT JOIN `postLang` `pl` ON pl.post_id=p1.id AND pl.lang_id=":lang"
WHERE p1.parent_id = :mid
jayant, your version also didn’t work.
PS. bennouna, don’t know. I’ll try to use a more reasonable alias names.
jayant
(Codesutras)
July 26, 2012, 1:04pm
9
elitsolutions:
Thank you, guys.
bennouna, I have tried your version but I got the below error:
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens. The SQL statement executed was: SELECT `p1`.`id`, `p1`.`title`
FROM `post` `p1`
LEFT JOIN `postLang` `pl` ON pl.post_id=p1.id AND pl.lang_id=":lang"
WHERE p1.parent_id = :mid
jayant, your version also didn’t work.
PS. bennouna, don’t know. I’ll try to use a more reasonable alias names.
ohh no porbs…;)but i will recommend you to always use Cdbcriteria for this kind of work.Which provide you more secure way.