amphp / mysql

An async MySQL client for PHP, optimizing database interactions with efficient non-blocking capabilities. Perfect for responsive, high-performance applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Insert invalid json value

rodion-k opened this issue · comments

Amp\Loop::run(function () {

    $db = new \Amp\Mysql\Pool(...);

    yield $db->query("CREATE TABLE IF NOT EXISTS amp_json (a varchar(255), b JSON)");

    /** @var \Amp\Mysql\Statement $statement */
    $statement = yield $db->prepare("INSERT INTO amp_json (a, b) VALUES (?, cast(? as json))");
    $json = json_encode([1,2,3]);
    yield $statement->execute([$json, $json]);

    print_r(yield \Amp\Iterator\toArray(yield $db->query('select * from amp_json')));

    $db->close();
});

Result is:

Array
(
    [0] => Array
        (
            [a] => [1,2,3]
            [b] => "base64:type251:WzEsMiwzXQ=="
        )
)

This query works as expected with PDO as well as with mysql console client

what is your expected result?

what is your expected result?

Array
(
    [0] => Array
        (
            [a] => [1,2,3]
            [b] => [1,2,3]
        )
)

@trowski I expected what solution for this issue would be inserting correct value to mysql, but now above code works, but I see in mysql console client the following:

+---------+-------------------------------+
| a       | b                             |
+---------+-------------------------------+
| [1,2,3] | "base64:type251:WzEsMiwzXQ==" |
+---------+-------------------------------+

Is this expected behavior?