PDO execute array to convert string

I get an array to convert a conversion error when I try to execute a PDO. Execution should introduce one, which is a normal string, and the other, an array. Here is my code:

$id = "1";    
$array = array("a",  "b",  "c");
$in  = str_repeat('?,', count($array) - 1) . '?';

$sql1 = "SELECT * FROM tbl1 AS tb1 RIGHT JOIN tbl2 AS tb2 ON (tb1.e_id = tb2.e_id)LEFT JOIN tbl3 AS tb3 ON (tb2.m_id = tb3.m_id) WHERE tb1.u_id = ? AND tb1.letters IN ($in)";


$statement1 = $db_handle->prepare($sql1);
$statement1->setFetchMode(PDO::FETCH_ASSOC);
$statement1->execute(array($id,$array));

$res = $statement1->fetchAll();

So the execution line gives me a string conversion error. I printed my array and question marks, and they give out perfectly. The sql statement is fine too, I tried it on phpMyAdmin and it works great, so I am not using it correctly, but I am not sure how to change the way it is executed.

Can someone explain this to me?

+4
source share
2 answers

execute()The method expects a single array. From the documentation:

. , :

  • ()

array($id,$array) , :

Array
(
    [0] => 1
    [1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

)

, , , . , , .

, . , :

$array = array(1, "a",  "b",  "c");

, array_unshift():

$id = "1";    
$array = array("a",  "b",  "c");
$array = array_unshift($array, $id);

... execute() :

$statement1->execute($array);
+5

->execute() , , ? .

2 , 2- ?, . , , .

:

$dataArray = $array;
array_unshift($dataArray, $id);

$statement1->execute($dataArray);
+1

Source: https://habr.com/ru/post/1529034/


All Articles