本文介紹了如何從 MySQL 中獲取整數(shù)作為 PHP 中的整數(shù)?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
當(dāng)數(shù)據(jù)從 MySQL 返回時(shí),無論 MySQL 數(shù)據(jù)類型如何,都會(huì)自動(dòng)以字符串形式返回.
When data is returned from MySQL, it is automatically returned as strings, regardless of the MySQL data type.
有什么方法可以告訴 MySQL/PHP 維護(hù)數(shù)據(jù)類型(例如 int),因此如果您查詢一個(gè) int 列,您會(huì)在 PHP 中得到一個(gè)整數(shù)而不是字符串?
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?
推薦答案
好吧,你可以做類型轉(zhuǎn)換 使用PHP轉(zhuǎn)換返回?cái)?shù)據(jù)的類型.
Well you can do Type casting to convert the type of returned data using PHP.
你可以看看int
,intval
轉(zhuǎn)換為整數(shù).你也可以使用 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;
這篇關(guān)于如何從 MySQL 中獲取整數(shù)作為 PHP 中的整數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!