Why does mysql change to the string type after php retrieves a field of the int type?

Source: Internet
Author: User
I connected using pdo and printed it using var_dump. I found that the field of the int type in mysql is printed and changed to the string type. I don't know what the problem is, is there a way for php to display the actual mysql field type? I connected using pdo and printed it using var_dump. I found that the field of the int type in mysql is printed and changed to the string type. I don't know what the problem is, is there a way for php to display the actual mysql field type?

Reply content:

I connected using pdo and printed it using var_dump. I found that the field of the int type in mysql is printed and changed to the string type. I don't know what the problem is, is there a way for php to display the actual mysql field type?

I tested it myself:
PHP-5.4.39 (built-in driver mysqlnd 5.0.10)

Create a test table and insert data:

create table test(    c1 int,     c2 float,     c3 float(10,2),     c4 double,     c5 double(10,2),     c6 decimal(10,2),     PRIMARY KEY (c1)) ENGINE=InnoDB DEFAULT CHARSET=utf8;insert into test values(32.10, 32.10, 32.10, 32.10, 32.10, 32.10);insert into test values(43.21, 43.21, 43.21, 43.21, 43.21, 43.21);insert into test values(9876543.21, 9876543.21, 9876543.21, 9876543.21, 9876543.21, 9876543.21);select * from test;+---------+---------+------------+------------+------------+------------+| c1      | c2      | c3         | c4         | c5         | c6         |+---------+---------+------------+------------+------------+------------+|      32 |    32.1 |      32.10 |       32.1 |      32.10 |      32.10 ||      43 |   43.21 |      43.21 |      43.21 |      43.21 |      43.21 || 9876543 | 9876540 | 9876543.00 | 9876543.21 | 9876543.21 | 9876543.21 |+---------+---------+------------+------------+------------+------------+

Output of the PDO query var_dump:


  $ App ['db _ pconnect '], PDO: ATTR_EMULATE_PREPARES => false, // note PDO: MYSQL_ATTR_INIT_COMMAND => 'set NAMES utf8 '));} catch (PDOException $ e) {echo $ e-> getMessage (); exit ();} $ something = $ dbh-> query ('select * FROM test '); $ arr = $……-> fetchAll (PDO: FETCH_ASSOC); $…… = null; $ dbh = null; var_dump ($ arr); // output: array (3) {[0] => array (6) {["c1"] => int (32) ["c2"] => float (32.099998474121) ["c3"] => float (32.099998474121) ["c4"] => float (32.1) ["c5"] => float (32.1) ["c6"] => string (5) "32.10"} [1] => array (6) {["c1"] => int (43) ["c2"] => float (43.209999084473) ["c3"] => float (43.209999084473) ["c4"] => float (43.21) ["c5"] => float (43.21) ["c6"] => string (5) "43.21"} [2] => array (6) {["c1"] => int (9876543) ["c2"] => float (9876543) ["c3"] => float (9876543) ["c4"] => float (9876543.21) ["c5"] => float (9876543.21) ["c6"] => string (10) "9876543.21" }}// if you set PDO: ATTR_EMULATE_PREPARES => true, the output is: array (3) {[0] => array (6) {["c1"] => string (2) "32" ["c2"] => string (4) "32.1" ["c3"] => string (5) "32.10" ["c4"] => string (4) "32.1" ["c5"] => string (5) "32.10" ["c6"] => string (5) "32.10"} [1] => array (6) {["c1"] => string (2) "43" ["c2"] => string (5) "43.21" ["c3"] => string (5) "43.21" ["c4"] => string (5) "43.21" ["c5"] => string (5) "43.21" ["c6"] => string (5) "43.21"} [2] => array (6) {["c1"] => string (7) "9876543" ["c2"] => string (7) "9876540" ["c3"] => string (10) "9876543.00" ["c4"] => string (10) "9876543.21" ["c5"] => string (10) "9876543.21" ["c6"] => string (10) "9876543.21 "}}

Whether PDO: ATTR_EMULATE_PREPARES is set to false or true,
The decimal () type is string, and the output data is correct.
When preprocessing is not simulated (false), the data type can be maintained, but some types of output data are inconsistent with the data in the database, such as the above float.
The types of fields returned by MySQLi query are also string.
Therefore, it is safe to return the string type to the program, and type conversion can be performed later:

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;

When you store data in the database, all types are converted into strings.

For example, can MySQL store data?

It must be converted to a string and then stored, for example, to Json or serialized.

Php is a weak type language, which may be converted to a string when you traverse the results.

Check the value assignment address of the variable.

Intval is required before warehouse receiving.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.