waza-ari / monolog-mysql

MySQL Handler for Monolog, which allows to store log messages to a MySQL Table
MIT License
141 stars 85 forks source link

PDOStatement::execute(): SQLSTATE[HY093] #41

Closed TwinMist closed 1 year ago

TwinMist commented 4 years ago

Hi Just had to revert back to 1.0.4 because in 1.0.5 was getting PHP Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in

any ideas

StephanKrauss commented 4 years ago

Hello,

i have the same problem. Here is my test setup

      ```

$pdo = new \PDO('mysql:host=localhost;dbname=monolog', 'monolog', 'monolog');

        $mysqlHandler = new \MySQLHandler\MySQLHandler($pdo, "log", array('channel', 'CORRELATION', 'errorFile', 'errorFile', 'errorLine', 'errorTrace'), \Monolog\Logger::INFO);

        $logger->pushHandler($mysqlHandler);

        $content = [
            'test1' => 'aaa',
            'test2' => 'bbb'
        ];

        $logger->error('Test', $content);

---------------------------

When i debug the script i find the query for mysql.
Query: INSERT INTO `log` (channel, level, message, time, channel, CORRELATION, errorFile, errorFile, errorLine, errorTrace) VALUES (:channel, :level, :message, :time, :channel, :CORRELATION, :errorFile, :errorFile, :errorLine, :errorTrace)

What is wrong ?

Your sincerly

Stephan
cr-lgl commented 4 years ago

@TwinMist @StephanKrauss I think I'll have time to check this out next week, please wait a little while.

nox-4O4 commented 4 years ago

Reverting commit 45a4bf3168c21b13272d16651f073a62bffd3cb0 fixes this for me.

For once, the check for unused keys is using a wrong array index: https://github.com/waza-ari/monolog-mysql/blob/45a4bf3168c21b13272d16651f073a62bffd3cb0/src/MySQLHandler/MySQLHandler.php#L210 This should probably be if(! isset($contentArray[$context])).

But the check does not seem to work correctly anyway as the insert statement is build and prepared prior to the unused keys getting removed. Thus, the statement always contains all additional fields/keys.

By (trying to) remove them after the statement has already been prepared, the parameters won't match up.

amacado commented 1 year ago

I've addressed this issue in https://github.com/waza-ari/monolog-mysql/pull/47/commits/4eb4ad35a16ecf5e16701326fb6a419bedee4552. It will be resolved in the upcomming release v2.