大网数据广告活动
服务器运维问答

Mariadb reading integers as strings

· 52CE 我爱测

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, Maria…

问题原文

原帖链接 · kirgy · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · kirgy · CC BY-SA 3.0

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/

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。