Yii Framework Forum: data binding - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

data binding Am I doing this right? Rate Topic: -----

#1 User is offline   thiswayup 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 20-July 10

Posted 23 July 2010 - 12:06 PM

Hi
Can someone verify I am binding my params correctly? I've read both http://www.yiiframew...i/CDbConnection and http://www.yiiframew...de/database.dao

$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();


It does not return a result array as expected.
0

#2 User is offline   Jaz Manister 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 27-October 09
  • Location:Rural Western New York, USA

Posted 23 July 2010 - 01:41 PM

View Postthiswayup, on 23 July 2010 - 12:06 PM, said:

$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();



Try this:
$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();

0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 24 July 2010 - 02:52 AM

Try bindValue() instead of bindParam().
0

#4 User is offline   GDzyne 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 15-June 09

Posted 24 July 2010 - 08:18 AM

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();

0

#5 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 24 July 2010 - 08:25 AM

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();

0

#6 User is offline   thiswayup 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 20-July 10

Posted 26 July 2010 - 03:29 AM

View PostY!!, on 24 July 2010 - 08:25 AM, said:

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

  
       $command = Yii::app() -> db -> createCommand($q);
        $command->bindValue(":areaSearch",$areaSearch);
        $results = $command->queryAll();


also what does the angled brackets in "%{$areaSearch}%" represent?
0

#7 User is offline   GDzyne 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 15-June 09

Posted 26 July 2010 - 04:45 AM

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';
......

0

#8 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 26 July 2010 - 05:35 AM

nice catch GDzyne...

so even the previous examples maybe would work but "=" should be changed to "LIKE"...

because "=" search exactly that character...

but

"LIKE" uses _ and % as two search helper symbol...
Find more about me.... btw. Do you know your WAN IP?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users