SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post please help for my search

    Hi, can i ask some help how can i do search using the LIKE operator in PDO.

    here is my code but it's not working

    Code:
            $strcon= $dbconn->prepare("SELECT employee_name
                              FROM employee where employee_name LIKE '%?%'
    
                              ");
            $strconn->bindParam(1,$querysearch);
            $strconn->execute();

    Thank you in advance

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    WAG here, but you can't use a ? in the substitute parm name along with %. Either a single ? or a :varname must be the pattern here.

    In your bind statement is where you use the %.

    For ex.:

    $strconn->bindparam(1,"%abc%");

    If that syntax even works for an sql LIKE clause.

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    It doesn't work

    $strconn->bindparam(1,"%abc%");

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Can we see you full code? One sample line is not very much.

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    444
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    And remember that with bindparam(), you don't want to have quotes around the parameter, because they're added for you:

    Code:
    $strcon= $dbconn->prepare("SELECT employee_name
                              FROM employee where employee_name LIKE ?");
            $strconn->bindParam(1,'%' . $querysearch . '%');
            $strconn->execute();
    If that doesn't help, show more code AND define "doesn't work" with a bit more detail please - do you get errors, or does it just not return anything?
    http://www.firenza.net - my homage to a car from the 1970s

  6. #6
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    HI,

    $strcon= $dbconn->prepare("SELECT employee_name
    FROM employee where employee_name LIKE ?");
    $strconn->bindParam(1,'%' . $querysearch . '%');
    $strconn->execute();
    This is the error.
    error = "only variables can be passed by reference";


    I fixed it by changing it to this way.

    $find = '%' . $querysearch . '%';

    $strcon= $dbconn->prepare("SELECT employee_name
    FROM employee where employee_name LIKE ?");
    $strconn->bindParam(1,$find);
    $strconn->execute();

    Thank you


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •