获取 MySQL 查询结果作为其本机数据类型? [英] Get MySQL Query Results as Their Native Data Type?

查看:27
本文介绍了获取 MySQL 查询结果作为其本机数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 mysql_fetch_row()mysql_result() 获取 MySQL 查询结果,并且数值以字符串形式返回.

I have tried fetching MySQL query results using mysql_fetch_row() and mysql_result() and numeric values are being returned as strings.

有什么办法可以将数据作为其存储在表中的数据类型来获取吗?

Is there any way to fetch the data as its datatype stored in the table?

该应用程序将查询许多不同的查询,因此我将无法将这些值逐一转换为预期的数据类型.

The application will be querying many different queries so I will be unable to cast the values as the intended datatype on a 1 by 1 basis.

推荐答案

我认为在 PHP 5.2 中无法以原生数据类型获取数据(即任何其他字符串)..

I don't think getting data in their native datatypes (i.e. anything else that strings) can be done in PHP 5.2...

在 PHP 5.3 中,如果我没记错的话,当您使用新的 (PHP 中的新 >= 5.3) mysqlnd (MySQL Native Driver) 司机.

In PHP 5.3, it becomes possible, if I remember correctly, when you are using the new (new as in PHP >= 5.3) mysqlnd (MySQL Native Driver) driver.

更多地翻阅我的书签后,我发现了这篇关于 mysqlnd 的文章:PDO_MYSQLND:PHP 5.3 中 PDO_MYSQL 的新特性

After more digging through my bookmarks I found this article about mysqlnd : PDO_MYSQLND: The new features of PDO_MYSQL in PHP 5.3

上面写着(引用):

使用mysqlnd进行PDO的优势

mysqlnd 在以下情况下返回本机数据类型使用服务器端准备好的语句,例如返回一个 INT 列作为整数变量而不是作为细绳.这意味着更少的数据内部转化.

mysqlnd returns native data types when using Server-side Prepared Statements, for example an INT column is returned as an integer variable not as a string. That means fewer data conversions internally.

但这仅适用于 PHP 5.3(前提是您的 PHP 5.3 版本是使用 mysqlnd(而不是旧的 libmysql)编译的),而且似乎仅适用于准备好的语句:-(

But this is PHP 5.3 only (provided your version of PHP 5.3 is compiled with mysqlnd (and not the old libmysql)), and seems to only be the case for prepared statements :-(

这并没有多大帮助,在你的情况下,我猜......

Which doesn't quite help, in your situation, I guess...


这是另一个,仍然是关于 mysqlnd 的新功能,它不仅讨论了准备好的语句:PHP:使用 mysqlnd 节省新的网络流量、CPU 和内存.

不确定这是否已合并到官方 mysqlnd 驱动程序中,但最好的方法是尝试;但它仍然只是 PHP >= 5.3,无论如何......

Not sure this has been merged into the official mysqlnd driver, though -- best way would be to try ; but it'll still be PHP >= 5.3 only, anyway...


另一种解决方案是在 PHP 端使用某种映射系统 (如 ORM) 将来自 DB 的结果转换为 PHP 数据类型...


Another solution would be to have, on the PHP-side, some kind of a mapping-system (like an ORM) to convert results coming from the DB to PHP datatypes...

是的,如果你想使用像 ===!== 这样的操作符,这很糟糕,它们是类型敏感的...

And yes, this is bad if you want to use operators like === and !==, which are type-sensitive...

这篇关于获取 MySQL 查询结果作为其本机数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆