1
votes

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.

1
I recommend passing the parameters in the execute unless you are using blobs. It works very reliably. - Ryan Vincent
@RyanVincent It may have something to do with Drupal's custom drivers. I've altered this query in several different ways. Is there a way to debug the PDO statement and get the driver configuration? - Roger
@RyanVincent here is the drupal documentation for PDO custom driver api.drupal.org/api/drupal/… it looks like they don't implement bindValue or bindParam. That could lead to the error I see. I'm going to look into Drupal configeration and figure out how I can use the default driver rather than the custom Drupal driver. - Roger
@RyanVincent The same error occurs. - Roger

1 Answers

1
votes

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