如何从 MySQL 中获取整数作为 PHP 中的整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5014522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get an integer from MySQL as integer in PHP?
提问by fbwb
When data is returned from MySQL, it is automatically returned as strings, regardless of the MySQL data type.
当数据从 MySQL 返回时,无论 MySQL 数据类型如何,它都会自动以字符串形式返回。
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?
有什么方法可以告诉 MySQL/PHP 维护数据类型(例如 int),因此如果您查询一个 int 列,您会在 PHP 中得到一个整数而不是字符串?
回答by Sarfraz
Well you can do Type castingto convert the type of returned data using PHP.
好吧,您可以使用 PHP进行类型转换以转换返回数据的类型。
You can take a look at int
, intval
to convert to integers. You may also use settype
:
您可以查看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");
Another way would be:
另一种方法是:
$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;
回答by aaz
In MySQLi use bind_result
: it sets the correct type and handles NULL
.
在 MySQLi 中使用bind_result
:它设置正确的类型和句柄NULL
。
回答by Gaurav Gupta
If you are running Ubuntu:
如果您正在运行 Ubuntu:
sudo apt-get remove php5-mysql
sudo apt-get install php5-mysqlnd
sudo service apache2 restart
回答by Stoosh
You could use type casting once you've pulled the data from your MySQL database
从 MySQL 数据库中提取数据后,您可以使用类型转换
$row['field'] = (int)$row['field'];
回答by John Parker
As PHP isn't a strongly typed language, this is somewhat meaningless, but you could of course simply cast the field in question to an int via settype, etc. prior to usage.
由于 PHP 不是强类型语言,因此这有点无意义,但您当然可以在使用前通过settype等简单地将有问题的字段转换为 int 。
Irrespective, there's no way (that I know of) to maintain this "type" information automatically.
无论如何,没有办法(据我所知)自动维护这种“类型”信息。
回答by Alex
PHP's default library for MySQL doesn't have any options of this kind. Use type casting like (int) or intval, (float) etc
PHP 的 MySQL 默认库没有任何此类选项。使用类型转换,如 (int) 或 intval, (float) 等
回答by quocnguyen
in php, you can use ([type]) to make sure $something was integer , this help sql injection if you khow about it.
在 php 中,您可以使用 ([type]) 来确保 $something 是 integer ,如果您了解它,这有助于 sql 注入。
ex:
前任:
(int) $something;
回答by Syed
I don't know in what context you asked this question. Because the data type of a php variable is dependent on what data type a column in your table has. If the column has data type "int" in mysql, you get an integer in php (not a string).
不知道你是在什么情况下问这个问题的。因为 php 变量的数据类型取决于表中列的数据类型。如果列在 mysql 中具有数据类型“int”,则在 php 中得到一个整数(不是字符串)。
But anyways, you can use either is_numeric(), is_int() or gettype() php functions to know the data type of a returned value from Mysql. The problem with is_numeric() is that it returns true even if a variable contains a numeric string e.g. "2". But gettype() will return "string" and is_int() will always return false in the example mentioned i.e. "2".
但无论如何,您可以使用 is_numeric()、is_int() 或 gettype() php 函数来了解 Mysql 返回值的数据类型。is_numeric() 的问题在于,即使变量包含数字字符串(例如“2”),它也会返回 true。但是 gettype() 将返回“string”,而 is_int() 在提到的示例中将始终返回 false,即“2”。
Once you know the data type of a returned value you can bind the php variable to maintain that type by type casting like this:
一旦您知道返回值的数据类型,您就可以绑定 php 变量以通过类型转换来维护该类型,如下所示:
// Suppose this is the returned value from mysql and
// you do not know its data type.
$foo = "2";
if (gettype($foo) == "string"){
$foo = (string) $foo;
}
You can make several checks for what data type the variable has and then cast it accordingly.
您可以对变量具有的数据类型进行多次检查,然后进行相应的转换。
I hope this reply would be helpful. Good luck! :)
我希望这个回复会有所帮助。祝你好运!:)
回答by Renan Coelho
If you want to get the values ordening by Price, you can change the Data Type to FLOAT from column (ex: price)
如果您想获得按价格排序的值,您可以将列中的数据类型更改为 FLOAT(例如:价格)
EX:
前任:
TABLE eletronics
表电子
price id
----------------
55.90 1
40.33 2
10.60 3
1596.90 4
56.90 5
PHP:
PHP:
$sql = $pdo->query("SELECT * FROM eletronics ORDER BY price DESC");
//result
//结果
1596.90 4
56.90 5
55.90 1
40.33 2
10.60 3
//The order is being per prices //OBS: You can change the dots with "," using the str_replace() //result: 1596,90 - 56,90 - 55,90 - 40,33 - 10,60 etc.
//订单是按价格计算的 //OBS:您可以使用 str_replace() 用 "," 更改点 //result: 1596,90 - 56,90 - 55,90 - 40,33 - 10,60 等.