0

I've run into an issue and getting this to work correctly is melting my brain. I'm wondering if anyone out there can point me in the right direction.

Basically, I'm using a multi threaded message queue handler to insert message values into a database. Everything works correctly, even the code below, but only on the initial insert. After the variables have been binded they keep the the same value, and will not reference the change in the $json object. I've tried several difference ways but I can't seem to get references to work. Any help would be appreciated!

private function handle_insert($message) {
    // declare data
    $json = json_decode($message->body);
    // prepare the statement
    if (!isset($this->statement)) $this->prepare_statement();
    // if there are no bindings
    if (!$this->binding) {
        // extract params
        extract(get_object_vars($json), EXTR_REFS);
        // loop over data
        foreach ($json as $key => $value) {
            // bind data
            $this->statement->bindParam(":{$key}", $$key, $this->pdo_param($$key)); // using function for defined types
        }
        // params are bound
        $this->binding = true;
    }
    // execute the statement
    $this->statement->execute();
}

I could bindParam on every insert, or even use bindValue. However, isn't the point of bindParam to only bind once and then change the value, reducing the need to loop?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

2 Answers2

0

I think this is an interesting issue, albeit irrelevant to PDO.

You have to think what values your extracted variables do reference. And then consider the level of over-engineering in your code.

And then move extract() outside the condition.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

It looks like class object is maintaining state of the variable $this->binding which you have already set in your first call as

$this->binding = true;

once binding value is set in first call then following condition prevents further binding of new variables to PDO object in subsequent calls.

if (!$this->binding)
MAK
  • 1