I have tired to run the code within my script and am getting error…the first error occur when I add “string” to getParams( string $params=NULL) ( Catchable fatal error: Argument 1 passed to getParams() must be an instance of string, string given, called in C:**\index.php on line 64 and defined in C:*\index.php on line 25 )
without "string " getParams( $params=NULL)
Error!: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
please kindly help me correct the code…Thanks in advance for your time.
//=================================
//
// Validate and clean input text
//
//=================================
function getParams( $params=NULL)
// :array
{
$result = NULL;
while (strpos($params, ' ') ) {
$params = str_replace(' ', ' ', $params);
}
$result = explode(' ', $params);
if( empty($result) ):
$result[] = $params;
endif;
return $result;
}//
//=================================
//
// dynamic build SQL statement
//
//=================================
function getSqlPdo(array $aParams=NULL)
// :string
{
$sql = 'SELECT * FROM business WHERE concat(business_name, " ", business_title, " " , business_description)';
foreach($aParams as $i2 => $param):
if($i2==0):
$sql = $sql .' LIKE ? ';
else:
$sql = $sql .' AND concat( business_name, " ", business_title, " " , business_description ) LIKE ? ';
endif;
endforeach;
// echo DEBUG, '$sql = ',$sql, '<br />';
return $sql;
}//
$aParams = getParams($_POST['keyword'] );
$sql = getSqlPdo( $aParams );
try {
include 'db.php';
$result = $pdo->prepare( $sql );
foreach($aParams as $aParamsValue ) {
$result->bindParam(':business_name', $aParamsValue, PDO::PARAM_STR);
$result->bindParam(':business_title', $aParamsValue, PDO::PARAM_STR);
$result->bindParam(':business_description', $aParamsValue, PDO::PARAM_STR);
$result->execute();
}
foreach ($result as $row){
$bizDetails[] = array(
'business_name' =>$row['business_name'],
'business_title' =>$row['business_title'],
'business_description' =>$row['business_description'],
'company_logo' =>$row['company_logo'],
'email_add' =>$row['email_add'] ); }
echo '<pre>';
print_r( $result );
echo '</pre>';
}catch (PDOException $e){
print "Error!: " . $e->getMessage() . "<br/>";
die();
}