Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

$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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
506 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...