我是PHP新手,我正在努力了解它是如何工作的。 我正在尝试将PDO实现到我自己的类中。
这就是我在做的事情:
class db {
private $options;
function __construct() {
$this->options = array(
'database_host' => DATABASE_HOST,
'database_name' => DATABASE_NAME,
'database_user' => DATABASE_USER,
'database_pass' => DATABASE_PASS
);
}
private function connect() {
try {
$pdo = new PDO('mysql:host=' . $this->options['database_host'] . ';dbname=' . $this->options['database_name'], $this->options['database_user'], $this->options['database_pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("set names utf8");
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
public function select($table, $what, $where, $custom) {
$query = "SELECT " . $what . " FROM " . $table;
$child = "1";
$param = array();
if ($where)
foreach ($where as $data => $value) {
if ($child == "1") {
$query .= " WHERE " . $data . " = '" . $value . "'";
$param[":" . $data] = $value;
$child = "next";
} else {
$query .= " AND " . $data . " = '" . $value . "'";
$param[":" . $data] = $value;
}
}
if ($custom)
$query .= ' ' . $custom;
$statement = $pdo->prepare($query);
if ($statement->execute($param))
return $statement->fetchAll(PDO::FETCH_ASSOC);
return false;
}
}
问题在于我不知道如何使函数connect()
与下面的select()
进行通信
当我尝试调用select()
函数时,我收到此错误:
Fatal error: Call to a member function prepare() on a non-object in /Applications/MAMP/htdocs/game/api/class.db.php on line 47
我希望当我调用select()
函数时,connect()
连接到数据库并使其所有变量可用于该类的其他函数...但我迷失了。
答案 0 :(得分:1)
将$pdo
变量存储为属性。目前它只是connect()
方法的本地变量。
class db {
private $options;
private $pdo;
}
然后使用$this->pdo
从任何方法访问它。例如,在connect()
:
private function connect() {
try {
$this->pdo = new PDO('mysql:host=' . $this->options['database_host'] . ';dbname=' . $this->options['database_name'], $this->options['database_user'], $this->options['database_pass']);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->exec("set names utf8");
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
对其他方法也这样做,例如:
$statement = $this->pdo->prepare($query);
您可能更喜欢扩展PDO类,而不是拥有一个保留PDO对象属性的新类。不同之处在于扩展它时,db
类的行为与PDO
类的行为相同,不同之处在于它将提供其他方法。对于您当前的代码,db
类只有您明确定义的方法。
答案 1 :(得分:0)
试试这个
class db {
private $options;
private $oPDO;
function __construct() {
$this->options = array(
'database_host' => DATABASE_HOST,
'database_name' => DATABASE_NAME,
'database_user' => DATABASE_USER,
'database_pass' => DATABASE_PASS
);
$this->connect();
}
private function connect() {
try {
$this->oPDO = new PDO('mysql:host=' . $this->options['database_host'] . ';dbname=' . $this->options['database_name'], $this->options['database_user'], $this->options['database_pass']);
$this->oPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->oPDO->exec("set names utf8");
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
public function select($table, $what, $where, $custom) {
$query = "SELECT " . $what . " FROM " . $table;
$child = "1";
$param = array();
if ($where)
foreach ($where as $data => $value) {
if ($child == "1") {
$query .= " WHERE " . $data . " = '" . $value . "'";
$param[":" . $data] = $value;
$child = "next";
} else {
$query .= " AND " . $data . " = '" . $value . "'";
$param[":" . $data] = $value;
}
}
if ($custom)
$query .= ' ' . $custom;
$statement = $this->oPDO->prepare($query);
if ($statement->execute($param))
return $statement->fetchAll(PDO::FETCH_ASSOC);
return false;
}
}