How to get an integer from MySQL as integer in PHP?(如何从 MySQL 中获取整数作为 PHP 中的整数?)
本文介绍了如何从 MySQL 中获取整数作为 PHP 中的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当数据从 MySQL 返回时,无论 MySQL 数据类型如何,都会自动以字符串形式返回.
When data is returned from MySQL, it is automatically returned as strings, regardless of the MySQL data type.
有什么方法可以告诉 MySQL/PHP 维护数据类型(例如 int),因此如果您查询一个 int 列,您会在 PHP 中得到一个整数而不是字符串?
Is there any way to tell MySQL/PHP to maintain the data types (e.g. int), so if you query an int column, you get an integer in PHP instead of a string?
推荐答案
好吧,你可以做类型转换 使用PHP转换返回数据的类型.
Well you can do Type casting to convert the type of returned data using PHP.
你可以看看int,intval 转换为整数.你也可以使用 settype:
settype($foo, "array");
settype($foo, "bool");
settype($foo, "boolean");
settype($foo, "float");
settype($foo, "int");
settype($foo, "integer");
settype($foo, "null");
settype($foo, "object");
settype($foo, "string");
另一种方式是:
$foo = (array)$foo;
$foo = (b)$foo; // from PHP 5.2.1
$foo = (binary)$foo; // from PHP 5.2.1
$foo = (bool)$foo;
$foo = (boolean)$foo;
$foo = (double)$foo;
$foo = (float)$foo;
$foo = (int)$foo;
$foo = (integer)$foo;
$foo = (object)$foo;
$foo = (real)$foo;
$foo = (string)$foo;
这篇关于如何从 MySQL 中获取整数作为 PHP 中的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:如何从 MySQL 中获取整数作为 PHP 中的整数?
基础教程推荐
猜你喜欢
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
