【问题标题】:PDO select statement stuck in the part where it asks databasePDO 选择语句卡在它询问数据库的部分
【发布时间】:2014-09-09 09:04:17
【问题描述】:

我的 PDO SELECT 有问题,我尝试从数据库中获取数据,但是当函数获取对PDO->prepare 的请求冻结并且没有任何反应时,好像卡在它询问数据库的部分,导致第一个 ECHO在职的。我尝试使用(column= 'value') 更改SQL 语法,但结果仍然相同。此源代码是我在此应用程序中使用 OOP 的类中函数的一部分。请帮忙,谢谢。 在var_dump() 没有任何显示看起来像函数没有通过 trought PDO。

public function changePswd($old_pswd, $new_pswd, $user_id) {
        echo $old_pswd . "<br/>" . $new_pswd . "<br/>" . $user_id;
        try {
            $stmt = $this->_db->prepare("SELECT * FROM zamestnanci WHERE id='" . $user_id . "'");
            $stmt->execute();
            if ($stmt->rowCount() > 0) {
                $hash_pswd = $stmt->fetch(PDO::FETCH_ASSOC);
                return $hash_pswd;
            }
        } catch (PDOException $e) {
            throw new Exception('DATABASE ERROR: ' . $e->getMessage());
        }
        var_dump($hash_pswd);

【问题讨论】:

  • 将您的throw new Exception 替换为echo。很可能您关闭了错误显示。
  • 在 catch 子句中抛出异常会使 try/catch 基本上无用。

标签: php mysql pdo


【解决方案1】:

我已经解决了这个问题,对不起我鲁莽了。问题出在 TRY/CATCH 的 TRY 部分的返回值中。因为 TRY/CATCH 在获取返回值时会破坏函数中的所有进度。

有一个成功:修改密码的功能(HASH + SALT)/comapre + update

public function changePswd($old_pswd, $new_pswd, $user_id) {
        try {
            $stmt = $this->_db->prepare("SELECT password,SALT FROM zamestnanci WHERE id='" . $user_id . "'");
            $stmt->execute();
            if ($stmt->rowCount() > 0) {
                $hash_pswd = $stmt->fetch(PDO::FETCH_ASSOC);
            }
        } catch (PDOException $e) {
            throw new Exception('DATABASE ERROR: ' . $e->getMessage());
        }
        $make_pword = sha1($hash_pswd['SALT']) . sha1($old_pswd);
        if ($make_pword === $hash_pswd['password']) {

            function rand_chars_salt($count = 8, $chars = 36) {
                $return = "";
                for ($i = 0; $i < $count; $i++) {
                    $rand = rand(0, $chars - 1);
                    $return .= chr($rand + ($rand < 10 ? ord('0') : ($rand < 36 ? ord('a') - 10 : ord('A') - 36)));
                }
                return $return;
            }

            $SALT = rand_chars_salt();
            $pass_finalize = sha1($SALT) . sha1($new_pswd);
            try {
                $this->_stmt = $this->_db->prepare("UPDATE zamestnanci SET `SALT`='" . $SALT . "',`password`='" . $pass_finalize . "' WHERE id='" . $user_id . "'");
                $this->_stmt->execute();
            } catch (PDOException $e) {
                throw new Exception('DATABASE ERROR: ' . $e->getMessage());
            }
            echo "<center><span><b>Heslo úspěšně změněno</b><span><center>";
        } else {
            echo "<center><span><b>Staré heslo se neshoduje</b><span><center>";

} }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 2012-04-28
    相关资源
    最近更新 更多