5

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?

2

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.