I'm building a search feature for my website where the user can search using 3 parameters. The user can choose to enter all 3 to filter the table or none at all and receive the whole table. I figured out how to dynamically build the query depending on what the user inputs but I'm having trouble calling bind_params() with the right number of arguments and the right order.
Code:
$sql = "SELECT position, rank, fullname, phonenumber, email, division
FROM `table` WHERE 1=1 ";
if(!empty($_POST['fname'])){
$firstname = $_POST['fname'];
$sql .= " AND `fullname` LIKE '%?%'";
}
if($_POST['div'] !== "All"){
$division = $_POST['div'];
$sql .= " AND `division` LIKE '%?%'";
}
if(!empty($_POST['pos'])){
$position = $_POST['pos'];
$sql .= " AND `position` LIKE '%?%'";
}
$stmnt = $db->prepare($sql);
$stmnt -> bind_param('sss', $firstname, $division, $position);
$stmnt -> bind_result($position, $rank, $fullname, $phonenumber, $email,$division);
$stmnt -> execute();
I always get this error: Number of variables doesn't match number of parameters in prepared statement I understand why I'm getting it but I tried multiple things but nothing worked out. Any hints or links to documentation would be useful. Thanks
array()structure toexecute().execute()with those values. Doing this withmysqlirequires low-level PHP hacking to pass in arbitrary arguments, it's a giant mess.bind_params. I'm looking at PDO now like @tadman suggested.