How to get an integer from MySQL as integer in PHP?

前端 未结 9 675
情话喂你
情话喂你 2020-12-31 03:26

When data is returned from MySQL, it is automatically returned as strings, regardless of the MySQL data type.

Is there any way to tell MySQL/PHP to maintain the data

9条回答
  •  遥遥无期
    2020-12-31 04:16

    Well you can do Type casting to convert the type of returned data using PHP.

    You can take a look at int, intval to convert to integers. You may also use 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;
    

提交回复
热议问题