Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepared query wrong number of parameter on 2.9.3 #47

Closed
2 tasks done
michalbundyra opened this issue Jan 16, 2020 · 6 comments · Fixed by #185
Closed
2 tasks done

Prepared query wrong number of parameter on 2.9.3 #47

michalbundyra opened this issue Jan 16, 2020 · 6 comments · Fixed by #185

Comments

@michalbundyra
Copy link
Member

Trying to execute a prepared query with named parameters. Works well with 2.9.2 but fails on 2.9.3.

Code to reproduce the issue

Something like:

$insert = $zdb->sql->insert(self::TABLE);
$insert->values(
    array(
        'name'  => ':name',
        'value'  => ':value'
    )
);
$stmt = $zdb->sql->prepareStatementForSqlObject($insert);

foreach ($values as $k => $v) {
    $stmt->execute(
        array(
            'name' => $k,
            'value' => $v
        )
    );
}

Expected results

Query success.

Actual results

Fails with the following exception:

==> An exception has been thrown in file tests/units/Preferences.php on line xx:
==> PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in vendor/zendframework/zend-db/src/Adapter/Driver/Pdo/Statement.php:239
==> Stack trace:
==> #0 vendor/zendframework/zend-db/src/Adapter/Driver/Pdo/Statement.php(239): PDOStatement->execute()
==> #1 lib/Preferences.php(310): Zend\Db\Adapter\Driver\Pdo\Statement->execute(Array)
[...]

Array passed to Statement::execute() looks like:

array(
    'name' => 'lang',
    'value' => 'en_US'
)

Thank you.


Originally posted by @trasher at zendframework/zend-db#322

@michalbundyra
Copy link
Member Author

The problem persists with zend-db 2.10.0.


Originally posted by @guillomovitch at zendframework/zend-db#322 (comment)

@trasher
Copy link
Contributor

trasher commented May 7, 2020

Problem seems to be named parameters, globally. This is still present with latest release.

Another example:

$update = $sql->update('mytable');
$update->set(
    array(
        'value'  => ':value'
    )
)->where->equalTo('name', ':name');
$stmt = $sql->prepareStatementForSqlObject($update);

In that example, trying to execute the statement with this array will fail:

array(
    ':name' => 'a name',
   ':value' => 'a value'
)

But that will works with:

array(
    ':c_0' => 'a name',
    ':where1' => 'a value'
)

or

array(
    'a name',
    'a value'
)

I really do not know if the problem is in prepareStatementForSqlObject, or in the parameters container that should to a kind of mapping.

@froschdesign
Copy link
Member

Please create a pull request with an unit test which illustrates the problem.

Thanks in advance!

@trasher
Copy link
Contributor

trasher commented May 7, 2020

I can take a look, but as I've said, I do not know what is expected here. Should the query be like:
1- UPDATE mytable SET value = :value WHERE name = :name
or
2- UPDATE mytable SET value = :c_0 WHERE name = :where1

@trasher
Copy link
Contributor

trasher commented May 7, 2020

OK, I've added a case to reproduce in integration tests. See #160

@trasher
Copy link
Contributor

trasher commented May 8, 2020

Maybe linked to zendframework/zend-db#35

weierophinney pushed a commit to weierophinney/laminas-db that referenced this issue Feb 22, 2021
closes laminas#47
add tests

Signed-off-by: Johan Cwiklinski <johan@x-tnd.be>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants