Home > Backend Development > PHP Tutorial > 这个是pdo的bug吗?

这个是pdo的bug吗?

WBOY
Release: 2016-06-06 20:23:37
Original
1331 people have browsed it

<code>$user='test';
$pass='123456';
$dbh = new PDO('mysql:host=127.0.0.1;dbname=test', $user, $pass);
$sth = $dbh->prepare("SELECT * FROM user WHERE id IN (?) ORDER BY id ASC");
$sth->execute(['456089015, 456089016, 456089017, 456089018, 456089019, 456089020, 456089021, 456089022, 456089023, 456089024, 456089025, 678689287']);
$result = $sth->fetchAll();
print_r($result);
</code>
Copy after login
Copy after login

其中只有678689287是存在的,结果返回为空;如果把678689287放在第一位,就有返回。

是不是个bug?

回复内容:

<code>$user='test';
$pass='123456';
$dbh = new PDO('mysql:host=127.0.0.1;dbname=test', $user, $pass);
$sth = $dbh->prepare("SELECT * FROM user WHERE id IN (?) ORDER BY id ASC");
$sth->execute(['456089015, 456089016, 456089017, 456089018, 456089019, 456089020, 456089021, 456089022, 456089023, 456089024, 456089025, 678689287']);
$result = $sth->fetchAll();
print_r($result);
</code>
Copy after login
Copy after login

其中只有678689287是存在的,结果返回为空;如果把678689287放在第一位,就有返回。

是不是个bug?

用预处理的的select...in语句,你得生成与查询数量相同的占位符

<code class="php">$queryParams = [456089015, 456089016, 456089017, 456089018, 456089019, 456089020, 456089021, 456089022, 456089023, 456089024, 456089025, 678689287];
$markers = str_repeat('?,', count($queryParams) - 1) . '?';
$st = $dbh->prepare("SELECT * FROM user WHERE id IN (${markers}) ORDER BY id ASC");
$st->execute($queryParams);
$result = $st->fetchAll();</code>
Copy after login

pdo不支持这种数组绑定

和我以前犯的错误一样,in后面的数值范围,有多少个数值,就来多少个问号,一个问号不行。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template