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



        $strcon= $dbconn->prepare("SELECT employee_name
                          FROM employee where employee_name LIKE '%?%'

                          ");
        $strconn->bindParam(1,$querysearch);
        $strconn->execute();



Thank you in advance:)

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.

Hi,
It doesn’t work

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

Can we see you full code? One sample line is not very much.

And remember that with bindparam(), you don’t want to have quotes around the parameter, because they’re added for you:


$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?

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 :slight_smile: