Why does Pdo convert all of our int type to string type ???

Source: Internet
Author: User
Why does Pdo convert all of our int type to string type ??? What parameters do you want to set ??

What are the considerations for this design ??? A little pitfall


Reply to discussion (solution)

You need to set the parameter type when passing the parameter. of course, you can set it to-1. have you set it?

You need to set the parameter type when passing the parameter. of course, you can set it to-1. have you set it? can you give me a sample code? Or where can I explain it?

Why does Pdo convert all of our int type to string type?

Does this code refer to bindParam?

When changing data between different applications, only text is passed.

What do you want to understand?

Why does Pdo convert all of our int type to string type?

Does this code refer to bindParam? Finally, the data returned from the database

When changing data between different applications, only text is passed.

I don't know what you want to understand. what do you mean?

Php automatically converts the data type according to the context. Don't bother you

Reference reply on the 4th floor:

When changing data between different applications, only text is passed.

What do you want to understand?
Which means I can only convert the type by myself?
All the data from the library is strings.

Php automatically converts the data type according to the context. Don't bother you

I cannot make a judgment =
If the two are to be calculated, it will not work!

I don't see the code. it's hard to guess here.

Here is an example.

Post Code

public function setGBK() {$sql = sprintf( "set names 'gbk'" );$this->conn->exec( $sql );}public function setBinary() {$sql = sprintf( "set character_set_client=binary" );$this->conn->exec( $sql );}    public function doExec($sql){        $sql = sprintf($sql);        $this->conn->exec( $sql );    }    public function query( $sql, $param ) {if( empty( $sql ) || empty( $this->conn ) ){return array();        }$result = $this->conn->prepare( $sql );if ( false == $result->execute($param) ){return array();}$result->setFetchMode( PDO::FETCH_ASSOC );$row = $result->fetch();if( $row ){return $row;}else {return array();}}public function querys( $sql, $param ) {if( empty( $sql ) || empty( $this->conn ) ){return array();        }$result = $this->conn->prepare( $sql );if ( false == $result->execute($param) ){return array();}$result->setFetchMode( PDO::FETCH_ASSOC );$rows = $result->fetchAll();return $rows;}

Except false =, there is nothing related to the data type.
Besides, false = writing is false =, so that it is actually related to the type.

Except false =, there is nothing related to the data type.
Besides, false = writing is false =, so that it is actually related to the type.

According to the principle "1" = 1, but here I return "1" = 1 is false, so intval is false.

This is not a problem

The problem is that it is a headache like "10"-5 in arithmetic operations. I just want to force intval.

Maybe there is something wrong with my code logic.

However, the string returned by the int type of the database such as pdo really brings me a lot of trouble.

Mysqli is okay.

The query results returned from the mysql database are all text and processed as strings, because PHP does not know your table structure. Both mysqli and pdo are like this!

This problem is quite tricky. I have not found a solution.

$ Db = new PDO ('MySQL: host = '. $ host.'; port = 3306; dbname = '. $ dbname, $ username, $ password,
Array (PDO: ATTR_PERSISTENT => TRUE, PDO: ATTR_STRINGIFY_FETCHES => FALSE ));
$ Db-> setAttribute (PDO: ATTR_STRINGIFY_FETCHES, FALSE );

$ Query = "SELECT 'name', 'boss', 'madetime', 'Rules', 'id' FROM 'Company "';
$ R = $ db-> query ($ query );

$ R-> setAttribute (PDO: ATTR_STRINGIFY_FETCHES, FALSE );

While ($ row = $ r-> fetch (PDO: FETCH_OBJ )){
$ Row ['id']; // type is string !!!!
}

It is said that the blue code does not automatically convert it to a string, but it is invalid. The red code directly tells me that it is not supported.

Don't tell me that PHP automatically converts the type to me. I want to convert $ row to JSON, all of which are converted to the string type ......

The column metadata returned by getColumnMeta (columnIndex) has the correct type, but there are two problems:
1. it uses the column index instead of the column name as the parameter, which is inconsistent with what I read
2. do not use this function on the official website. it may make specification changes without notice at any time.

Solve this problem successfully and report it.

First, it cannot be solved in the previous MySQL. It directly returns a string to the external system. A slightly newer MySQL and client drivers can directly convert internal local types to strings instead. With this foundation, it is possible to solve the problem.

Secondly, PDO: ATTR_STRINGIFY_FETCHES is irrelevant to MySQL. use PDO: ATTR_EMULATE_PREPARES (set to FALSE) to tell MySQL not to perform conversion.

Finally, PDO: ATTR_STRINGIFY_FETCHES is used to tell PHP not to perform conversion.

Now, the problem is solved.

This problem was also encountered during JSON Conversion. later we found that json_encode () has a JSON_NUMERIC_CHECK parameter.

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.