0

I am trying to use prepare statement to query data from the database. The output of this query is an empty array, which should not be the case. I don't get an error.

$starting = 0;
$ending = 40;
$condition = 1;
//$arr_rows in an array filled with integer values.
$in = join(',', array_fill(0, count($arr_rows), '?'));
$sql = "SELECT `row_id`, `col_name` FROM `table_name` 
        WHERE `condition`=? AND row_id IN ($in)
        ORDER BY row_id DESC
        LIMIT ?, ?";
$stmt = $connect->prepare($sql);
$params = array_values($arr_rows);
array_unshift($params, $condition);
array_push($params,$starting,$ending);
$stmt->execute($params);

$res= $stmt->fetchAll(PDO::FETCH_OBJ);
var_dump($res);

Debugging the code gives this:

SELECT `row_id`, `col_name` FROM `table_name` WHERE condition='1'                 
                        AND row_id IN ('334','333')
                        ORDER BY row_id DESC
                        LIMIT '0', '40'

I ran the same code directly in the database and found that the problem was caused by the values passed to LIMIT. These are of the string type. By removing the quote around the numbers => LIMIT 0, 40, the query works fine and the two expected rows are returned with "row_id = {334, 333}".
I have even converted the parameters to integer values using intval() before adding them to the array, but the problem persists. How can I solve this problem?

Kyv
  • 615
  • 6
  • 26

0 Answers0