Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO fetchAll() returns an empty array

Tags:

php

mysql

bind

pdo

in my code im trying to get data from my db with PDO and bind params but i keep on getting empty array, this is my code :

try{
    $pdo =new PDO('mysql:host=localhost;dbname=***', '***','***');
    $pdo->setAttribute(pdo::ATTR_ERRMODE,
                  pdo:: ERRMODE_EXCEPTION);
    $pdo->query('set names "utf8"');
}
catch (PDOException $e) {
   die('error connectin database');
}
$table = 'products';
$column = 'id';
$niddle = '70';
$sql = "SELECT * FROM `{$table}` WHERE ";
$sql .= ":column LIKE :niddle";
$pre = $pdo->prepare($sql);
$pre->bindParam(':column', $column ,PDO::PARAM_STR);
$pre->bindParam(':niddle', $niddle, PDO::PARAM_STR);
$result = $pre->setFetchMode(PDO::FETCH_ASSOC);
$pre->execute();
print_r($pre->fetchAll());

there is no exeption thrown, what could be the problem?

like image 919
user2326568 Avatar asked Nov 13 '22 06:11

user2326568


1 Answers

You should not bind the column name as a prepared statement parameter string as it will quote the column name. Do like you do with the table name just use it-- after whitelisting it.

like image 164
Ray Avatar answered Nov 15 '22 06:11

Ray