检查SQL查询的多个值

时间:2021-07-08 02:07:17

How to check a multiple values on a SQL query? Code:

如何检查SQL查询的多个值?码:

$value1 = '...';
$value2 = '...';

$statement = $db->prepare('SELECT * 
    FROM table
    WHERE entry = :value1;');       

$statement->bindValue(':value1', $value1 , PDO::PARAM_STR);

Current code checks only the first value.

当前代码仅检查第一个值。

1 个解决方案

#1


0  

use IN clause:

使用IN子句:

$values = array(1,2,3,4);
$stmt = $pdo->prepare("SELECT * FROM T 
    WHERE id IN (" . str_pad('', count($values)*2-1,'?,') . ")");
$stmt->execute($values);

also you can use
implode(',', array_fill(1, count($values),'?')),
str_repeat('?,',count($values)-1).'?'

你也可以使用implode(',',array_fill(1,count($ values),'?')),str_repeat('?,',count($ values)-1)。'?'

Idea is clear - repeated qustions)

想法很清楚 - 重复的问题)

#1


0  

use IN clause:

使用IN子句:

$values = array(1,2,3,4);
$stmt = $pdo->prepare("SELECT * FROM T 
    WHERE id IN (" . str_pad('', count($values)*2-1,'?,') . ")");
$stmt->execute($values);

also you can use
implode(',', array_fill(1, count($values),'?')),
str_repeat('?,',count($values)-1).'?'

你也可以使用implode(',',array_fill(1,count($ values),'?')),str_repeat('?,',count($ values)-1)。'?'

Idea is clear - repeated qustions)

想法很清楚 - 重复的问题)