Skip to content
Advertisement

Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in PDO Insert Query

I am using PDO to execute a insert query but it is throwing me this error I am not able to understand what the problem is! Please help me out! thank you in advance:)

    $count_query_params = [$fromDate, $toDate, $TRNO];
    $count_query = "INSERT INTO `$HA_ENTRIES`(`fromDate`, `toDate`, `user_trno`) VALUES (?,?,?)";
    $count_row = db_query_all($count_query_params, $count_query);

// DB_Query_ALL function

function db_query_all($arrayParams, $query)
{
global $conn;

$result = $conn->prepare($query);
foreach ($arrayParams as $param) {
    $result->execute([$param]);
}

$rows = array();
while ($row = $result->fetch()) {
    $rows[] = $row;
}
if (count($rows) < 1) {
    $rows = false;
}
return $rows;

}

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in……

db_query_all(Array, ‘INSERT INTO `14…’) #2 {main}…..

Advertisement

Answer

On your sample code I think that you must call execute() only once. So, istead of:

foreach ($arrayParams as $param) {
    $result->execute([$param]);
}

Try:

 $result->execute($arrayParams); 
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement