Type conversion from mysqli and pdo queries

Source: Internet
Author: User
There is always a problem, that is, PHP uses mysqli or pdo. The queried results are all int-type data that will automatically become string-type, and PHP itself is weak. However, it is not feasible to write the api directly from the database and then return the json_encode to the client. Java and...
  1. There is always a problem, that is, PHP uses mysqli or pdo. The queried results are all int-type data that will automatically become string-type, and PHP itself is weak. However, it is not feasible to write the api directly from the database and then return the json_encode to the client. Both java and oc are strongly typed numbers and numbers, while strings are strings. 123! = "123"

This is the program code, using the mysqli class

This is the table structure

This is the result of the query.

  1. Google has a lot of arguments, but pdo has a solution to the problem of converting the pdo query int to a string. However, mysqli does not find it and does not know what the problem is. Or are there some ready-made class libraries that can be automatically converted.

Reply content:
  1. There is always a problem, that is, PHP uses mysqli or pdo. The queried results are all int-type data that will automatically become string-type, and PHP itself is weak. However, it is not feasible to write the api directly from the database and then return the json_encode to the client. Both java and oc are strongly typed numbers and numbers, while strings are strings. 123! = "123"

This is the program code, using the mysqli class

This is the table structure

This is the result of the query.

  1. Google has a lot of arguments, but pdo has a solution to the problem of converting the pdo query int to a string. However, mysqli does not find it and does not know what the problem is. Or are there some ready-made class libraries that can be automatically converted.

When simulated preprocessing is disabled, the data type can be maintained, but some types of output data are inconsistent with the values in the database:
For example, for a float (9, 2) field, the field content is 32.10, the value read when PDO disables simulated preprocessing is 32.099998474121, And the type is float.
That is to say, when PDO analog preprocessing is enabled (default), the returned data types are all strings and the values are consistent with those stored in the database.
When the MySQL server does not use preprocessing, data of the string type is returned.

MySQLi does not use the prepare query to return all the fields of the string type, the value is consistent with the content stored in the database.
When prepare is used, similar to when pdo_mysql disables analog preprocessing, data of the corresponding type is returned (the decimal type is still a string), and the float type values are inconsistent.

Therefore, we recommend that you use the string or decimal type to save data with decimal points in the database.

Type conversion:
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;

Availablemysqli_resultObjectfetch_fieldTo obtain the metadata of a field. See PHP: mysqli_result: fetch_field-Manual.

Related Methodsfetch_fields,fetch_field_direct.

Here is an example:

// $ Types = array (); while ($ finfo = $ result-> fetch_field () {if (in_array ($ finfo-> type, array (MYSQLI_TYPE_BIT, struct, struct, MYSQLI_TYPE_LONG, MYSQLI_TYPE_LONGLONG, MYSQLI_TYPE_INT24) {$ type = 'int';} else if (in_array ($ finfo-> type, array (struct, MYSQLI_TYPE_DOUBLE, MYSQLI_TYPE_DECIMAL, MYSQLI_TYPE_NEWDECIMAL) {$ type = 'float ';} else {// For other types to be converted, Please study the type Constants on your own. // For details about the type constants, see http://php.net/manual/zh/mysqli.constants.php. For more information, see $ type = 'string ';} $ types [$ finfo-> name] = $ type;} $ result = $ result-> fetch_assoc (); foreach ($ result as $ name => & $ value) {settype ($ value, $ types [$ name]);} // the Data Type in $ result already meets the requirements

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.