$arr = array();
$from_date = '2015-01-01';
$to_date = '2015-01-31';
$order_no = '25215';
$sql = "SELECT * FROM test";
if(!empty($from_date)&&!empty($to_date))
{
$sql.=" WHERE txn_date BETWEEN :from_date AND :to_date";
$arr[] = ":from_date => $from_date";
$arr[] = ":to_date => $to_date";
$condition=true;
}
if(!empty($order_no))
{
if($condition)
{
$sql.=" AND ref_number = :order_no";
$arr[] = ":order_no => $order_no";
}
else
{
$sql.=" WHERE ref_number = :order_no";
$arr[] = ":order_no => $order_no";
$condition=true;
}
}
$stmt = $db->prepare($sql);
$stmt->execute($arr);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
When executing this query shows a warning like
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
what is wrong with it?
ref_number LIKE :order_nomight as well beref_number = :order_noseeing as you have no wildcard characters