用两个 类 查询 PDO
Query PDO with two classes
晚上好,我对从两个 classes 创建的 PHP PDO 查询有疑问。这是用于数据库连接的第一个 class:
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 的菜鸟,我是来学习的,我花了将近 3 个小时才弄清楚如何解决,现在我想问:我做错了什么我的代码有什么问题?非常感谢您。
绑定参数的PDO方法是bindParam
,不是bind_param
。而你需要使用fetch
来读取一行结果。如果 SELECT
.
没有匹配的行,它将 return false
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";}
//...
}
晚上好,我对从两个 classes 创建的 PHP PDO 查询有疑问。这是用于数据库连接的第一个 class:
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 的菜鸟,我是来学习的,我花了将近 3 个小时才弄清楚如何解决,现在我想问:我做错了什么我的代码有什么问题?非常感谢您。
绑定参数的PDO方法是bindParam
,不是bind_param
。而你需要使用fetch
来读取一行结果。如果 SELECT
.
false
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";}
//...
}