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

查看:165
本文介绍了获取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?

应用程序将查询许多不同的查询,因此我将无法以1比1的方式将值强制转换为预期的数据类型.

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本机驱动程序)驱动程序.

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: The new features of PDO_MYSQL in PHP 5.3

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的新功能的内容,它不仅针对准备好的语句进行讨论:


And here's another one, still about the new features of mysqlnd, which talks about this for not only prepared statements : PHP: New network traffic, CPU and memory savings with mysqlnd.

不过,不确定是否已将其合并到正式的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天全站免登陆