Pdo为何把我们int型都转换成string型了,能不能让他不转???
要设置什么参数??
这个设计是基于什么考虑???有点小坑爹啊
回复讨论(解决方案)
你需要在传参时候设置它的类型,当然了,可以设置成-1,你设置了吗
你需要在传参时候设置它的类型,当然了,可以设置成-1,你设置了吗 不明白 能给个示例代码吗?或者 哪个地方有讲解给个地址
Pdo为何把我们int型都转换成string型了
这块的代码是指bindParam吗?
在不同的应用程序间就换数据时,传递的只能是文本。
不清楚你想弄明白什么
Pdo为何把我们int型都转换成string型了
这块的代码是指bindParam吗? 最后从数据库返回的数据
在不同的应用程序间就换数据时,传递的只能是文本。
不清楚你想弄明白什么 意思说我只能自己转换类型了?
php 会根据上下文自动转换数据类型。不需劳你费神
引用 4 楼 的回复:
在不同的应用程序间就换数据时,传递的只能是文本。
不清楚你想弄明白什么
意思说我只能自己转换类型了?
从库里出来的都是字符串
php 会根据上下文自动转换数据类型。不需劳你费神
我做判断的是 不行了 ==
再如果 两者要做 计算就不行了!
代码没看到,在这里真难猜。
给点实例啊,楼主
请帖出代码
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; }
除了 false == 外,没有什么与数据类型的相关的
况且 false == 应写作 false === ,这样才真正与类型相关
除了 false == 外,没有什么与数据类型的相关的
况且 false == 应写作 false === ,这样才真正与类型相关
按照原理 "1" == 1 但是 我这里 return "1"==1 就是false,非得 intval哈才是false
这里不是问题
问题是做算术运算的是 “10”- 5 这样的头疼,我都是强制intval了就好了
也许是我的代码逻辑哪里有问题吧
但是pdo这种 数据库的int返回的string的情况真的给我带来不少麻烦
mysqli 可没这样的问题
从mysql数据库返回的查询结果都是文本,都会被当成字符串处理,因为PHP并不知道你的表结构。 mysqli和pdo都是这样!
这个问题是挺操蛋的。我也没有找到解决办法。
$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!!!!
}
据说,蓝色代码是设定它不自动转换为字符串的,但是,无效。红色代码运行时直接告诉我不支持。
别告诉我说PHP自动给我转换类型的屁话。我要把$row转换为JSON,都转换成字符串类型了……
getColumnMeta(columnIndex)返回的列元信息里面倒是有正确的类型,但是有两个问题:
一、它采用列索引而不是列名作为参数,跟我的读取凡是不符合
二、官网上说不要使用这个函数,它可能在任何时候不做任何通知而进行规格更改
成功解决这个问题,汇报一下。
首先,在以前的MySQL中还真是不能解决的。它直接返回字符串给外部系统。稍微新一点的MySQL和客户端驱动可以直接内部的本地类型而不再进行内部转换为字符串了。有了这个基础,就有解决的可能了。
其次,PDO::ATTR_STRINGIFY_FETCHES对于MySQL来说是无关的,要使用PDO::ATTR_EMULATE_PREPARES(设为FALSE)来告诉MySQL端不要进行转换。
最后,当然是用PDO::ATTR_STRINGIFY_FETCHES告诉PHP也不要进行转换了。
至此,问题解决。
同样是转 JSON 时遇到这个问题, 后来发现 json_encode() 有一个参数 JSON_NUMERIC_CHECK.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
