PHP DB连接和密码验证

时间:2013-09-02 09:49:17

标签: php mysql database

我有三个错误

  

警告:mysqli_stmt :: fetch()完全需要0个参数,给出1   /Volumes/shared/Digital/_Websites/_TEST/qpm/classes/mysql.php在线   20

     

注意:尝试获取非对象的属性   /Volumes/shared/Digital/_Websites/_TEST/qpm/classes/mysql.php在线   23

     

注意:尝试获取非对象的属性   /Volumes/shared/Digital/_Websites/_TEST/qpm/classes/mysql.php在线   23

这是我的代码

<?php 
require_once 'includes/constants.php';
class mysql{
    private $conn;

    function __construct(){
        $this->conn = $conn = new MySQLi(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME)
            or die ('There was an error in the connection');
        }

    function verify ($un, $pwd){


            $username = $un;
            $password = $pwd;
            if ($sth = $this->conn->prepare("SELECT pass FROM User WHERE username = '".$un."' LIMIT 1")) {
            $sth->execute();

            $user = $sth->fetch(PDO::FETCH_OBJ);

            // Hashing the password with its hash as the salt returns the same hash
            if (crypt($password, $user->hash) == $user->hash) {
              return true;
            } else { 

                return false; }

            }//end of if;

        }//end of verify

    }//enfd of class

只是试图获得传递,如果相同则返回true,否则返回false

由于

1 个答案:

答案 0 :(得分:0)

像很多很多其他php用户一样,你会混淆2个完全不同的API - mysqli和PDO。

请选择一个,即PDO,并使您的代码与之保持一致。

这里的代码包含了所有无用的东西,
但是有了适当的东西,即准备好的陈述,补充道:

function verify ($un, $pwd)
{
    $sql = "SELECT pass FROM User WHERE username = ?"
    $sth = $this->conn->prepare($sql);
    $sth->execute(array($un));
    $pass = $sth->fetchColumn();
    return (crypt($pwd, $pass) == $pass);
}

但请注意verify的此功能不应该是mysql类的方法