Neoaptt February 2016

Drupal 8 PDO Prepared Statements bindValue error

I am having trouble with this query. This query results in the following error.

PDOException: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound

$sql = "INSERT IGNORE table SET user_id = :uid";
if ($con = $connection->prepare($sql)) {
  $con->bindValue(':uid', intval($this->uid), PDO::PARAM_INT);
  $con->debugDumpParams();
  $con->execute();
}

Parameters

Params:  1
Key: Name: [4] :uid
paramno=-1
name=[4] ":uid"
is_param=1
param_type=1

Query That works.

$sql = "INSERT IGNORE table SET user_id = :uid";
if ($con = $connection->prepare($sql)) {
    $con->execute([':uid' => intval($this->uid)]);
}

Question Why can't I bind any parameter or value to the PDO prepared statement?

I am using Drupal 8 and I believe that their connections replace the PDO default driver.

Answers


Neoaptt February 2016

Drupal 8 replaces the default PDO driver with a custom driver that Drupal uses to insert, update, prepare statements, ect... These drivers override the default operations. So if you want a complete customize query you will need to set up your own connection separate from Drupal and use that.

$dsn = 'mysql:host=localhost;dbname=drupal';
$username = 'username';
$password = 'password';
$connection = new PDO($dsn, $username, $password);

Then using the custom queries will work. and you won't get the no parameters were bound error

Post Status

Asked in February 2016
Viewed 2,132 times
Voted 4
Answered 1 times

Search




Leave an answer