escaping column name with PDO

前端 未结 3 1464
温柔的废话
温柔的废话 2020-12-03 15:24

I have a function that\'s like

function getInfoById($id, $info) {

}

the idea is to have a query be \"SELECT $info FROM table WHERE i

3条回答
  •  感情败类
    2020-12-03 16:21

    I would just filter it out with some regex. Keep it simple.

    Also, you should bind $id and have it be :id

    $info = preg_replace('/[^A-Za-z0-9_]+/', '', $info);
    
    $stmt = $pdo->prepare('SELECT $info FROM table WHERE id = :id'); 
    $stmt->bindParam(':id', $id);
    $stmt->execute();
    

提交回复
热议问题