ODBC连接仅返回字符串作为数据类型 [英] ODBC connection returns only strings as data type

查看:67
本文介绍了ODBC连接仅返回字符串作为数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用PHP和MS Access.我有一个具有不同数据类型的简单表.与数据库的连接工作良好,但是,如果对查询执行var_dump,则会注意到所有数据类型都转换为字符串. 如果我将同一数据库与ASP一起使用,则数据类型正确.该问题似乎仅与PHP和ODBC驱动程序有关.
有什么方法可以获取具有正确数据类型的值?
任何帮助将不胜感激.

I'm currently working with PHP and MS Access. I have a simple table with different data types. The connection to the database works good but, if I do a var_dump to the query, I notice that all data type are all converted to strings. If I use the same database with ASP I got the data types correct. The problem seems to be only with PHP and the ODBC driver.
Is there any way to get those values with their correct data type?
Any help would be greatly appreciated.

推荐答案

PHP中的Access ODBC连接确实将所有字段作为字符串返回,例如

While it is true that an Access ODBC connection in PHP returns all fields as string, e.g.

array(5) {
  ["ID"]=>
  string(1) "1"
  ["TextField"]=>
  string(13) "This is text."
  ["IntegerField"]=>
  string(1) "3"
  ["DateTimeField"]=>
  string(19) "2014-03-01 00:00:00"
  ["CurrencyField"]=>
  string(8) "100.0000"
}

没关系,因为当您在计算中使用字符串时,PHP会将字符串转换为数字. (或者,您可以按照此处中的说明进行显式转换. )

it really doesn't matter because PHP will just cast the strings to numbers when you use them in a calculation. (Or, you can explicitly cast them as explained here.)

唯一需要特殊处理的字段是日期/时间字段,然后您只需将它们传递给strtotime()即可将它们转换为Unix时间戳值:

The only fields that require special handling are the Date/Time fields, and then all you need to do is pass them to strtotime() and they will be converted to Unix timestamp values:

$data = odbc_fetch_array($result);
$datetime = strtotime($data["DateTimeField"]);
echo '$datetime value is: ' . $datetime;
echo "\r\n\r\n";
echo '$datetime formatted with date("c", ...) is: ' . date("c", $datetime);

产生

$datetime value is: 1393650000

$datetime formatted with date("c", ...) is: 2014-03-01T00:00:00-05:00

这篇关于ODBC连接仅返回字符串作为数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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