通过PDO参数化SELECT查询?

时间:2012-12-11 02:24:15

标签: php pdo

  

可能重复:
  How to prevent SQL injection?

我正在试图弄清楚如何通过执行SELECT的PDO进行参数化查询,现在我已经得到了这段代码:

function user_login($username, $password) {
    $conn = connection_getConnection();

    $stmt = $conn->prepare("SELECT `password` FROM `users` WHERE `username` = :username");
    $row = $stmt-> #WHAT DO I DO HERE?
    if (empty($row)) {

    }
}

所以,我对这条线路做了评论。请从这里帮助我。

谢谢!

1 个答案:

答案 0 :(得分:2)

PHP Manual有一些很好的例子。

对你:

function user_login($username, $password) {
    $conn = connection_getConnection();

    $sql = "SELECT `password` FROM `users` WHERE `username` = :username";
    $stmt = $conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    $query = $stmt->execute(array(':username' => $username));
    $rows = $query->fetchAll();
    if (empty($rows)) {

    }
}