PHP使用预准备语句检查WHERE子句中的字符串

时间:2011-05-09 15:50:47

标签: php pdo prepared-statement

我正在尝试为准备好语句的用户插入时间戳。但是,idHash是一个字符串,它永远不会在正确的行中插入字符串。当表中的idHash为空时,这些行会受到影响。我如何告诉PHP我希望将其视为字符串??

function setLastRefresh($idHash)
{
    global $dbUser;
    global $dbPass;

    // check if the user is in any of the other tables or if sb is trying to hack the db

    $db = new PDO('mysql:host=localhost;dbname=myDB', $dbUser, $dbPass);
    $preparedStatement = $db->prepare("update users set lastRefresh = NOW() WHERE idHash=:idHash");
    $preparedStatement->execute(array(':idHash' => $idHash));
    $preparedStatement->closeCursor();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

绑定参数时,可以指定数据类型。您想使用PDO::PARAM_STR类型。

有关示例,请参阅http://www.php.net/manual/en/pdostatement.bindparam.php。你必须改变你指定参数和执行一些的方式。

相关问题