catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

has() method has a debug when set ATTR_EMULATE_PREPARES to false #756

Closed ha-ni-cc closed 5 years ago

ha-ni-cc commented 6 years ago

Medoo version 1.5.7 Mysql version 5.7

` $medoo = new Medoo([

            'database_type' => DB_CONFIG['TYPE'],

            'database_name' => DB_CONFIG['DB'],

            'server' => DB_CONFIG['HOST'],

            'port' => DB_CONFIG['PORT'],

            'username' => DB_CONFIG['USER'],

            'password' => DB_CONFIG['PWD'],

            'charset' => DB_CONFIG['CHARSET'],

            'prefix' => DB_CONFIG['PREFIX'],

            'logging' => DEBUG_MODEL,

            'option' => [PDO::ATTR_EMULATE_PREPARES => false]

        ]);

`

has() method always return FALSE.

but I modified the function, it could work well.

` public function has($table, $join, $where = null) { $map = []; $column = null;

    $query = $this->exec('SELECT EXISTS(' . $this->selectContext($table, $map, $join, $column, $where, 1) . ')', $map);

    if ($query)
    {
        $result = $query->fetchColumn();

        return $result === 1 || $result === '1' || $result === true;
    }

    return false;
}

`

Old code:

return $result === '1' || $result === true;

To this:

return $result === 1 || $result === '1' || $result === true;

Because I setting ATTR_EMULATE_PREPARES to FALSE, the COLUMN return value's type is integer. but you strict judge with string and boolean. So it always return FALSE.

PLEASE TO FIXED IT , THANKS!

catfan commented 6 years ago

Do not open a new duplicated issue from #720. You can discuss on there.

I tested this case again with another environment, finally reproduced the problem.

I will test more and figure out for the EMULATE_PREPARES option and find a way to fix it.

ha-ni-cc commented 6 years ago

720 has been discussed for a long time,but I can't see the solution.

This debug affected many application scenarios,so I hope the next version can repair it! @catfan

catfan commented 6 years ago

It just because you set the ATTR_EMULATE_PREPARES to false. The default value of this option is true.

There is no problem using the default value. We do not recommend to change those PDO option. It may cause some unexpected problem.

If you just want to convert the type of fetched value, Medoo has provided a way to convert it.

For this case, it need to test other PDO option together for compatible reason for the next.

catfan commented 5 years ago

This has been fixed on a951846f9bfe93419d40700d9fbefde5c9b10259.