使用两个类查询PDO

时间:2015-02-12 17:11:18

标签: php mysql class pdo

晚上好,我对从两个类创建的PHP PDO查询有疑问。这是用于数据库连接的第一个类:

class PDOConn {
    protected $HOST = "host";
    protected $DATABASE = "db";
    protected $USER = "usr";
    protected $PASSWORD = "pwd";
    protected $CONN;

    public function __construct() {
        $this->CONN = new PDO("mysql:host=".$this->HOST.";dbname=".$this->DATABASE,$this->USER,$this->PASSWORD);
    }
}

第二个用于注册和登录用户:

class insLog{
    protected $db;
    protected $TheHash = "PASSHASH";

    function __construct(){
        $db = $this->db = new PDOConn();
    }

    function __destruct(){
        $this->db = null; 
    }

    public function reg($nome,$cognome,$password,$email){
        $stmt = $this->db->prepare("SELECT id FROM utenti WHERE cognome = :cognome OR email = :email");
        $stmt->bind_param(array(":cognome"=>cognome,":email"=>email));
        $stmt->execute();
        //Here I wanna use if row instead if there's id so > 0...
        echo $stmt['id'];
        if($stmt['id'] == ""){echo "Lol";}
        //...
    }
} 

请记住我是OOP进入OOP而且我在这里学习并且差不多3个小时我想弄清楚如何解决,现在我想问:我做错了什么是什么我的代码有问题吗?非常感谢你。

1 个答案:

答案 0 :(得分:0)

绑定参数的PDO方法是bindParam,而不是bind_param。您需要使用fetch来读取一行结果。如果false没有匹配任何行,它将返回SELECT

public function reg($nome,$cognome,$password,$email){
    $stmt = $this->db->prepare("SELECT id FROM utenti WHERE cognome = :cognome OR email = :email");
    $stmt->bindParam(array(":cognome"=>cognome,":email"=>email));
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    if(!$row){echo "Lol";}
    //...
}