2

I have a local MySQL environment on an Ubuntu instance, and a live MariaDB environment running on a Centos 7 instance. When selecting an integer field from a table using PHP, this value should return as an integer, however, MariaDB returns this value as a string.

Both environments are running the same instance of code using Laravel Eloquent ORM, and I have checked each respective database is storing the value as integers/tiny-ints etc.

The code used is:

\App\Item::where('id','=',1)->first()->key_item;

Mysql, Ubuntu:

0

MariaDB, Centos 7:

"0"

This is weird behaviour to me, and on the programming end, is causing errors when evaluating the expected integers in "if" expressions.

Is there some configuration option that can force MariaDB to return the integer values as integer types?

7
  • Could you add the code: How you select it using PHP? May 26, 2017 at 10:06
  • @EsaJokinen I've added some further details. This is using the Eloquent ORM via Laravel.
    – kirgy
    May 26, 2017 at 10:14
  • What happens if you query the database directly using a SQL client, rather than using Laravel?
    – Barmar
    May 26, 2017 at 18:52
  • PHP will happily convert strings to integers when necessary, so why is this a problem?
    – Barmar
    May 26, 2017 at 18:52
  • @Barmar because when you have two environments exhibiting two different behaviours this is a problem. If a database returns a string for a boolean type this is a problem. The only solution for a Laravel Eloquent build is to manually cast each database attribute using the $cast attribute on the models.
    – kirgy
    May 26, 2017 at 22:14

1 Answer 1

3

After experimenting with solutions on a virtual Centos 7 machine, I discovered the solution was to replace the PHP-MySQL plugin layer of the database with the PHP MySQL plugin with the native driver.

This issue is not actually to do with MariaDB nor MySQL, but rather the PHP driver. Swapping out MariaDB for MySQL persisted this problem. The Centos 7 shipped driver for MariaDB-PHP will cast integers to strings, and the only database-side solution is to swap out the driver.

More information about the native driver can be found here: https://dev.mysql.com/downloads/connector/php-mysqlnd/

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.