doumu9019 2014-01-14 01:49
浏览 27
已采纳

使用PDO而不绑定

$stmt = $conn->prepare('SELECT * FROM users WHERE user_id = :user_id');

$stmt->execute(array(':user_id' => $_GET['user_id']));

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

I'm using PDO like that, do I need to sanitise GET parameter?

I know if I do $stmt->bindParam(':user_id', $_GET['user_id'], PDO::PARAM_INT); than it is not a problem. But is my way safe?

  • 写回答

1条回答 默认 最新

  • dtyqeoc70733 2014-01-14 01:51
    关注

    Yes, it's safe. The only differences between execute and bind* are:

    • execute accepts several parameters at once, while you have to bind* each one individually
    • bind* allows you to specify the parameter type, while execute binds everything as strings

    Passing parameters to execute is mostly a convenience shorthand, it's still safe.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部