本文介紹了如何從 MySQL 中獲取整數作為 PHP 中的整數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
當數據從 MySQL 返回時,無論 MySQL 數據類型如何,都會自動以字符串形式返回.
When data is returned from MySQL, it is automatically returned as strings, regardless of the MySQL data type.
有什么方法可以告訴 MySQL/PHP 維護數據類型(例如 int),因此如果您查詢一個 int 列,您會在 PHP 中得到一個整數而不是字符串?
Is there any way to tell MySQL/PHP to maintain the data types (e.g. int), so if you query an int column, you get an integer in PHP instead of a string?
推薦答案
好吧,你可以做類型轉換 使用PHP轉換返回數據的類型.
Well you can do Type casting to convert the type of returned data using PHP.
你可以看看int
,intval
轉換為整數.你也可以使用 settype
:
settype($foo, "array");
settype($foo, "bool");
settype($foo, "boolean");
settype($foo, "float");
settype($foo, "int");
settype($foo, "integer");
settype($foo, "null");
settype($foo, "object");
settype($foo, "string");
另一種方式是:
$foo = (array)$foo;
$foo = (b)$foo; // from PHP 5.2.1
$foo = (binary)$foo; // from PHP 5.2.1
$foo = (bool)$foo;
$foo = (boolean)$foo;
$foo = (double)$foo;
$foo = (float)$foo;
$foo = (int)$foo;
$foo = (integer)$foo;
$foo = (object)$foo;
$foo = (real)$foo;
$foo = (string)$foo;
這篇關于如何從 MySQL 中獲取整數作為 PHP 中的整數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!