簡體   English   中英

類型為int(11)的數據庫值作為字符串返回

[英]Database value with type int(11) is returned as string

我有一個名為customer_id的mysql數據庫表列,其數據類型為int(11)。

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

我得到的結果是字符串

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

返回否。

為什么將整數值作為字符串返回?

更新:我使用PDO作為access_layer。

它從PDO作為字符串返回。 is_int()不會測試為true,但是,如果您使用is_numeric() ,它將為true,如果它是一個數字字符串。

$myint = '1';
echo is_int($myint) ? 'yes' : 'no'; //returns 'no'
echo is_numeric($myint) ? 'yes' : 'no'; //return 'yes'

您可以做的一件事是

var_dump($customers); die();

這將為您提供從數據庫返回的所有類型。 過去使用PDO時,對於返回字符串應為INT的字符串,我沒有遇到任何問題。

作為最后的選擇,您始終可以在需要時將其強制轉換為INT。

(!is_int($customers['customers_id'])) ? (int)$customers['customers_id'] : $customers['customers_id'] ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM