我对PHP很新;但是我觉得我对它有很好的把握,但是我有一个问题,理解为什么我无法访问第二版连接到DB类的连接。
我在配置脚本中全局连接到第一个。
$db = new db(array('login info'));
我使用以下内容在我的控制器中访问它。
public $db;
public function __construct(){
$this->db = $GLOBALS['db'];
}
然后在我的控制器中我有一个函数,我在其中创建一个新的实例来连接到另一个DB。
$ordersDB = new db(array('login info'));
现在我想使用
之类的东西单独访问每个数据库$this->db->select("SELECT * FROM ada Yada
和
$ordersDB->select("SELECT * FROM Yada Yada
然而,两者仍在访问第一个数据库。我知道这是因为我在执行第二个时遇到一个表不存在错误,它告诉我它的查询是什么db。然而,它们的var_export显示它们实际上是不同的!我完全误解了课程是如何工作的,或者我一次只能打开一个数据库?谢谢你的帮助。
编辑:这是你要找的吗?
$db = new db(array(connect info));
$controller = new controller();
$controller->connectToSecondDB();
class db {
public $dbHost;
public $dbUser;
public $dbName;
public $dbPassword;
public function __construct() {
$arguments = func_get_args();
if(!empty($arguments)){
foreach($arguments[0] as $key => $property){
if(property_exists($this, $key)){
$this->{$key} = $property;
}
}
}
}
public function connect() {
if(!isset(self::$connection)) {
self::$connection = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName);
}
}
class controller {
public $db;
public function __construct(){
$this->db = $GLOBALS['db'];
}
public function connectToSecondDB(){
$ordersDB = new db(array(connect info));
$ordersDB->select("SELECT * FROM `customer` WHERE email = 'email@address.com' LIMIT 1")
$this->db->query("SQL");
}
}
编辑选择查询功能
private function _sel($table, $where="", $order_by="", $limit="", $group_by="",$database = NULL){
if($database === NULL) { $database = $this->db; }
if($limit == 1){ $single = true; }else{ $single = false; }
//if( is_array($where) ){ $this->_buildWhere(); }
$where = (strlen($where) > 0) ? "WHERE $where " : $where;
$group_by = (strlen($group_by) > 0) ? "GROUP BY $group_by " : $group_by;
$order_by = (strlen($order_by) > 0) ? "ORDER BY $order_by " : $order_by;
$limit = (strlen($limit) > 0) ? "LIMIT $limit " : $limit ;
$sql = "SELECT *
FROM `$table`
$where
$group_by
$order_by
$limit";
// Debug
//if(INCLUDE_CHECK){ echo "<HR>".$sql."<HR>"; }
$results = $database->select($sql);
if($single && count($results) > 0 ){
return($results[0]);
}else{
return($results);
}
}
答案 0 :(得分:1)
解决方案是重用$ controller对象。添加query
方法。然后,您可以独立于连接方法运行查询,以便可以重用控制器对象
您需要在整个过程中的每个步骤进行错误检查。
请注意,我删除了$this->db->query("SQL");
,因为我不确定它究竟是做了什么。答案中的概念应该让您开始重新设计您的课程。
<强>更新强> 根据评论,我从超级全局更改$ db并将其传递给控制器对象。
$db = new db(array(connect info));
// try passing the $db object so it does not need to be a super global
$controller = new controller($db);
// You can keep your $controller object as a global - I use a session variable for this to reuse it between pages.
$controller->connectToSecondDB();
// run the query separate from the connection so the controller object can be reused
$result = $controller->ordersQuery("SELECT * FROM `customer` WHERE email = 'email@address.com' LIMIT 1")
class db {
public $dbHost;
public $dbUser;
public $dbName;
public $dbPassword;
public $connection;
public function __construct() {
$arguments = func_get_args();
if(!empty($arguments)){
foreach($arguments[0] as $key => $property){
if(property_exists($this, $key)){
$this->{$key} = $property;
}
}
}
}
public function connect() {
if(!isset($this->$connection)) {
$this->connection = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName);
}
}
class controller() {
public $db;
public $ordersDB; // ADD THIS
public function __construct($db2){
$this->db = $db2;
}
public function connectToSecondDB(connect info){
$ordersDB = new db(array(connect info));
}
public function ordersQuery($sql){
$this->ordersDB->query($sql)
// Fetch and return you result set here
}
}