$q = "SELECT areaName as `name`,para1 as descrp, para2,image as img
FROM areas
WHERE `areaName` = \"%:areaSearch%\";";
$command = Yii::app() -> db -> createCommand($q);
$command->bindParam(":areaSearch",$areaSearch,PDO::PARAM_STR);
$dataReader = $command-> query();
$results=$dataReader->readAll();
$q = 'SELECT areaName as name, para1 as descrp, para2, image as img from areas where areaName = ":areaSearch"';
$command = Yii::app()->db->createCommand($q);
$command->bindParam(':areaSearch', $areaSearch);
$results = $command->queryAll();
Binding a parameter will quote the placeholder based on the underlying driver so one should not place any qoutes around the placeholder.
eg.
$q = 'SELECT areaName as name, para1 as descrp, para2, image as img from areas where areaName = :areaSearch';
$command = Yii::app()->db->createCommand($q);
$command->bindParam(':areaSearch', $areaSearch);
$results = $command->queryAll();
But he wants to add % before and after search term.
Try
$q = 'SELECT areaName as name, para1 as descrp, para2, image as img from areas where areaName = :areaSearch';
$command = Yii::app()->db->createCommand($q);
$command->bindValue(':areaSearch', "%{$areaSearch}%");
$results = $command->queryAll();
Y!! is right, I wish to do a wildcard search. I’m still having trouble running the line above. I just get back an empty array. But when I run it without it works ie
Hi thiswayup, Angled brackets tell the PHP parser to separate the contents (variable) from the rest of the string.
Try setting it up this way :
$q = 'SELECT areaName as name, para1 as descrp, para2, image as img from areas where areaName = :areaSearch';
$command = Yii::app()->db->createCommand($q);
$areaSearch = '%'.$areaSearch.'%';
$command->bindParam(':areaSearch', $areaSearch);
$results = $command->queryAll();
I have used bindParam in this instance so one must make sure to set the variable before passing it the method (as in the example above)
Actually, taking a look at your sql statement you have specified the incorrect operand (=) it should be (LIKE)
eg:
.....
$q = 'SELECT areaName as name, para1 as descrp, para2, image as img from areas where areaName LIKE :areaSearch';
......