MYSQL - PDO with multiple IN clause

Ka Tech

I can successfully implement a IN clause within a PDO prepared statement using the following code.

in_array = array(1,2,3);
$in  = str_repeat('?,', count($in_array) - 1) . '?';
$sql = "SELECT * FROM my_table WHERE my_value IN ($in)";
$stm = $db->prepare($sql);
$stm->execute($in_array);
$data = $stm->fetchAll();

How can I do the same for multiple $in? For example I've unsuccessfully tried the following:

in_array1 = array(1,2,3);
$in1  = str_repeat('?,', count($in_array) - 1) . '?';
in_array2 = array(4,5,1);
$in2  = str_repeat('?,', count($in_array) - 1) . '?';
$sql = "SELECT * FROM my_table WHERE (my_value1 IN ($in1)) AND (my_value2 IN ($in2))";
$stm = $db->prepare($sql);
$stm->execute($in_array1,$in_array2);
$data = $stm->fetchAll();

I think its got to do with stm->execute but not sure, help appreciated

chris85

Your current query comes out as

SELECT * FROM my_table WHERE (my_value1 IN (?,?,?)) AND (my_value2 IN (?,?,?))

So your execute usage is incorrect, http://php.net/manual/en/pdostatement.execute.php. It should only be passing one array with values inside it.

An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR.

I think using array_merge, http://php.net/manual/en/function.array-merge.php, will allow you to accomplish what you are trying

$stm->execute(array_merge($in_array1,$in_array2));

This way the execute is the equivalent of

$stm->execute(array(1,2,3,4,5,1));

This may seem incorrect because the array pairings are now gone but placeholder 1 (the first question mark) will map to 1, placeholder 4 to 4, and so on.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

when in clause with mysql pdo

From Dev

PDO Prepared Statements with multiple conditions in where clause

From Dev

Multiple where in clause in Mysql

From Dev

Multiple queries/results (PDO/MySQL)

From Dev

MySQL multiple tables in the WHERE clause

From Dev

mysql where clause using 'IN" or multiple 'OR'

From Dev

MySQL: Multiple columns for group clause

From Dev

PHP PDO error when using placeholders in the LIMIT clause of a MySQL query

From Dev

PHP MySQL PDO TextArea Where clause with condition checks

From Dev

PDO: Multiple commands in PDO::MYSQL_ATTR_INIT_COMMAND

From Dev

PDO MySQL SELECT with multiple criteria within an IF statement

From Dev

Multiple MYSQL Updates Failing in PDO Transaction

From Dev

Multiple ways to identify user / mysql, pdo, php

From Dev

PDO Insert multiple checkbox values in Mysql

From Dev

Same array multiple times in MySQL "IN" query with PDO

From Dev

PHP, PDO, MySQL - Multiple INSERT vulnerable to injection?

From Dev

MySQL JOIN QUERY with MULTIPLE WHERE CLAUSE

From Dev

MySQL Multiple Conditions on Group By / Having Clause

From Dev

Where clause in multiple joins in mysql query

From Dev

MySQL multiple columns in IN clause with null values

From Dev

Update query with multiple table in where clause in MySql

From Dev

MySQL JOIN QUERY with MULTIPLE WHERE CLAUSE

From Dev

mysql, multiple table Joins with WHERE clause

From Dev

php pdo where clause

From Dev

PDO not recognising NOT EXISTS clause

From Dev

PDO / mySQL How to bind multiple values to store multiple rows

From Dev

how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

From Dev

How to split a multi-value WHERE clause into multiple primitives to use with PDO prepared statements in PHP

From Dev

get first insert id for multiple insert using pdo in mysql

Related Related

  1. 1

    when in clause with mysql pdo

  2. 2

    PDO Prepared Statements with multiple conditions in where clause

  3. 3

    Multiple where in clause in Mysql

  4. 4

    Multiple queries/results (PDO/MySQL)

  5. 5

    MySQL multiple tables in the WHERE clause

  6. 6

    mysql where clause using 'IN" or multiple 'OR'

  7. 7

    MySQL: Multiple columns for group clause

  8. 8

    PHP PDO error when using placeholders in the LIMIT clause of a MySQL query

  9. 9

    PHP MySQL PDO TextArea Where clause with condition checks

  10. 10

    PDO: Multiple commands in PDO::MYSQL_ATTR_INIT_COMMAND

  11. 11

    PDO MySQL SELECT with multiple criteria within an IF statement

  12. 12

    Multiple MYSQL Updates Failing in PDO Transaction

  13. 13

    Multiple ways to identify user / mysql, pdo, php

  14. 14

    PDO Insert multiple checkbox values in Mysql

  15. 15

    Same array multiple times in MySQL "IN" query with PDO

  16. 16

    PHP, PDO, MySQL - Multiple INSERT vulnerable to injection?

  17. 17

    MySQL JOIN QUERY with MULTIPLE WHERE CLAUSE

  18. 18

    MySQL Multiple Conditions on Group By / Having Clause

  19. 19

    Where clause in multiple joins in mysql query

  20. 20

    MySQL multiple columns in IN clause with null values

  21. 21

    Update query with multiple table in where clause in MySql

  22. 22

    MySQL JOIN QUERY with MULTIPLE WHERE CLAUSE

  23. 23

    mysql, multiple table Joins with WHERE clause

  24. 24

    php pdo where clause

  25. 25

    PDO not recognising NOT EXISTS clause

  26. 26

    PDO / mySQL How to bind multiple values to store multiple rows

  27. 27

    how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

  28. 28

    How to split a multi-value WHERE clause into multiple primitives to use with PDO prepared statements in PHP

  29. 29

    get first insert id for multiple insert using pdo in mysql

HotTag

Archive