class dbconnect extends PDO {
private $dbengine = 'mysql';
private $dbhost = 'localhost';
private $dbuser = 'root';
private $dbpassword = 'root';
private $dbname = 'nesiojami';
public $dbh = null;
public function __construct() {
try {
$pdo = $this->dbh = new PDO("".$this->dbengine.":host=$this->dbhost; dbname=$this->dbname", $this->dbuser, $this->dbpassword);
$pdo = $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
catch (PDOException $e) {
$e->getMessage();
}
}
}
这就是我连接数据库的方式。
$clients = $pdo->query("SELECT client-name, client-surname FROM clients")->fetchAll(PDO::FETCH_ASSOC);
我找不到如何通过连接数据库来获取数据的方法。
答案 0 :(得分:0)
在dbconnect类
的构造函数中调用PDO构造函数$con= dbconnect new ();
$clients = $con->query("SELECT client-name, client-surname FROM clients")->fetchAll(PDO::FETCH_ASSOC);
阅读此question 了解更多信息
答案 1 :(得分:0)
class DbConnect extends PDO {
private $dbengine = 'mysql';
private $dbhost = 'localhost';
private $dbuser = 'root';
private $dbpassword = 'root';
private $dbname = 'nesiojami';
public $dbh = null;
public function __construct() {
try {
// since you are extending PDO, you have to call its constructor
parent::connect($this->dbengine.":host=$this->dbhost; dbname=$this->dbname", $this->dbuser, $this->dbpassword);
$this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
}
像这样使用:
$pdo = new DbConnect;
$clients = $pdo->query("SELECT client-name, client-surname FROM clients")->fetchAll(PDO::FETCH_ASSOC);