1

我有一个名为customers_id 的mysql 数据库表列,其数据类型为int(11)。

    echo gettype($customers['customers_id']);

结果我得到了字符串

    echo is_int($customers['customers_id']) ? 'yes' : 'no';

返回没有。

为什么整数值作为字符串返回?

更新:我PDO用作 access_layer。

4

2 回答 2

1

它作为字符串从 PDO 返回。is_int()不会测试为真,但是如果你使用is_numeric()它,如果它是一串数字,它将测试为真。

$myint = '1';
echo is_int($myint) ? 'yes' : 'no'; //returns 'no'
echo is_numeric($myint) ? 'yes' : 'no'; //return 'yes'
于 2013-09-27T13:54:46.350 回答
-3

你可以做的一件事是

var_dump($customers); die();

这将为您提供从数据库返回的所有类型。在过去使用 PDO 时,我没有遇到字符串返回到它们应该是 INT 的问题。

作为最后的手段,您总是可以在需要时将其转换为 INT。

(!is_int($customers['customers_id'])) ? (int)$customers['customers_id'] : $customers['customers_id'] ;
于 2013-09-27T13:51:52.040 回答