How to use both condition and like operator in PDO mysql


#1

I am trying to use like operator with condition but it is showing null values

Just like operator is working

$query = $conn->prepare(‘SELECT * FROM user_registration user_name LIKE ? and user_email LIKE ? and user_mobile LIKE ?’);
$query->bindValue(1, “%$f_name%”, PDO::PARAM_STR);
$query->bindValue(2, “%$emailn%”, PDO::PARAM_STR);
$query->bindValue(3, “%$mobilen%”, PDO::PARAM_STR);
$query->execute();


but i want to add created_by = 2 in the query
i tried like this but it wont work (showing null values)

$query = $conn->prepare(‘SELECT * FROM user_registration WHERE created_by = ? and user_name LIKE ? and user_email LIKE ? and user_mobile LIKE ?’);

$query->bindParam(4, $created_by );
$query->bindValue(1, “%$f_name%”, PDO::PARAM_STR);
$query->bindValue(2, “%$emailn%”, PDO::PARAM_STR);
$query->bindValue(3, “%$mobilen%”, PDO::PARAM_STR);

$query->execute();

what i did wrong…?

thank you,


#2

Check this out https://phpdelusions.net/pdo#like


#3

You prepared four parms and bound only three of them… Look again…


#4

You are mis-numbering the bound parameters/values. The position of the place-holders in the sql query statement must match the parameter number you are using in the bindParam/bindValue calls.

I recommend that you forget about explicitly binding data and just supply an array of values to the ->execute() method call. This results in the simplest and least amount of code.

$query = $conn->prepare('SELECT * FROM user_registration WHERE created_by = ? and user_name LIKE ? and user_email LIKE ? and user_mobile LIKE ?');

$query->execute([$created_by,"%$f_name%","%$emailn%","%$mobilen%"]);