I am trying to implement dynamic sql query so that users has flexibility to use one or more filters to dig out the data he wants. I am using prepared statements to ensure that there is no SQL Injection. I am using WAMP Server 64 bit. I referred to this article for writing my code: Sample
Following is my code:
$myqry="SELECT * FROM students WHERE ";
$initflag=0; //controls the first clause without AND and rest with AND
$paramtypes=array();
$paramvalues=array();
$params=array();
if(!empty($_POST['awr_admyear']))
{
$myqry.= "YEAR(adm_date)=? ";
$initflag=1;
$paramtypes[]='s';
$paramvalues[]=$_POST['awr_admyear'];
}
if(!empty($_POST['awr_admfrom']) && !empty($_POST['awr_admto']))
{
if($initflag==0)
$myqry.= "YEAR(adm_date) BETWEEN ? AND ? ";
else
$myqry.= "AND YEAR(adm_date) BETWEEN ? AND ? ";
$initflag=1;
$paramtype[]='s';
$paramtype[]='s';
$paramvalues[]=$_POST['awr_admfrom'];
$paramvalues[]=$_POST['awr_admto'];
}
if(!empty($_POST['awradm_no']))
{
if($initflag==0)
$myqry.= "adm_no LIKE ? ";
else
$myqry.= "AND adm_no LIKE ? ";
$initflag=1;
$paramtype[]='s';
$paramvalues[]=$_POST['awradm_no'];
}
$params = array_merge($paramtype,$paramvalues);
if(isset($myqry) && !(empty($myqry)))
{
if($result1 = $mysqli->prepare($myqry))
{
call_user_func_array(array($result1, 'bind_param'), $params);
if($result1->execute())
{
$finrest=$result1->get_result();
while($row= $finrest->fetch_assoc())
{
//and so on
I am getting the following error:
Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in..................
Where I am going wrong and what is the solution?