Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';t在子类中重置我的数据库连接_Php_Oop - Fatal编程技术网

Php Can';t在子类中重置我的数据库连接

Php Can';t在子类中重置我的数据库连接,php,oop,Php,Oop,我有三门课: 这是主要的DB类-if将在其他两个类中使用 class db { const DBHOST = 'Localhost'; const DBNAME = 'project'; const DBUSER = 'root'; const DBPASS = ''; public $db=array(); private $_dpt = array('fr'=>'','en'=>'_en'); public $db_pref = 'fr'; public $db_name; p

我有三门课: 这是主要的DB类-if将在其他两个类中使用

class db {
const DBHOST = 'Localhost';
const DBNAME = 'project';
const DBUSER = 'root';
const DBPASS = '';
public $db=array();
private $_dpt = array('fr'=>'','en'=>'_en');
public $db_pref = 'fr';
public $db_name;

public function __construct(){

}
public function connect_db(){
    if(!isset($this->db[$this->db_pref])){
        $this->db[$this->db_pref] = mysql_connect (self::DBHOST, self::DBUSER, self::DBPASS) or die (mysql_errno.' : Can`t connect to this DB');
        mysql_select_db(self::DBNAME.$this->_dpt[$this->db_pref], $this->db[$this->db_pref]) or die (mysql_errno.' : Can`t select this DB');
        mysql_query("SET NAMES utf8");
    }
}
这是我的crud课程:

 require_once('db.php');
 class crud extends db {    
public function __construct(){
    $this->lang = $_SESSION['user_lang_type']; // fr or en
    parent::__construct();
    if(isset($p['db']) && isset($p['db_pref'])){
        // I put here old db conn. to new object for economy
        $this->db_pref=$p['db_pref'];
        $this->db=$p['db'];
        if(isset($p['prevent']) && $p['prevent']!=$p['db_pref']){
           // if isset db_prevent - I connect to the project_fr db only 
           $this->db_pref=$p['prevent'];
           $this->connect_db();
        }
    } else $this->connect_db();
}
// reset prevented database
public function resetPreventDB($db=false){
  $this->db_pref=$this->lang;
  if($db) foreach($db as $k=>$v){ if(!isset($this->db[$k])) $this->db[$k]=$v; }
}
public function getData(){
    // this is shorted version of my crud query 
    // here I use link to selected db $this->db[$this->db_pref]
    return mysql_query('select * from advertice',$this->db[$this->db_pref]);
}   
} 下面是我的第三个类-它使用CRUD类的连接从选定的数据库加载数据:

class Advertice extends crud {
private $route;
public $cont;

public function __construct(){
    global $crud;
    parent::__construct(array('db'=>$crud->db,'db_pref'=>$crud->db_pref,'prevent'=>'fr'));
    $this->getList($p);
}

public function getList(){
    $q = $this->getData();
    global $crud;  
    // this didnt work - if db is FR it can be changed to EN
    $crud->resetPreventDB($this->db);
}

}
include_once 'crud.php';
include_once 'advertice.php';
$crud = new crud();
$crud->set = array();
// For ex. now I am in project_en database
$crud->getData() - will return data from project_en
$crud->set['ads'] = new Advertice(); // get data from project_fr
$crud->getData() // will return data from project_fr - but I call $crud->resetPreventDB() in  Advertice->getList().

我真的不知道为什么$crud->resetPreventDB()不起作用-但我真的不喜欢这段代码,因为这个bug停止了我所有初创公司的开发。非常感谢您的理解和帮助)谢谢

Mate,您正在调用函数
getData()
,但在此函数中没有对
resetPreventDB()
函数的引用

为什么要更改数据库

也许您想调用函数
getList()
,而不是
getData()

顺便说一句:如果你的代码不使用globals,看起来会更好,比如:

公共函数getList(){
$q=$this->getData();
$this->resetPreventDB($this->db);
返回$q;
}

作为一般建议-考虑使用任何PHP框架。 所有类似的问题很久以前就解决了,许多开发人员对其进行了广泛的测试。
在初始学习曲线之后,您将能够以至少10倍的速度发展。


如果您需要关于特定框架的建议,请询问。

是的-使用PHP框架是一个好主意,但是项目太大了,我们没有时间重新键入所有代码)我找到了-这个错误是因为PHP在mysql\u query()活动数据库连接链接中使用的,在mysql\u select\u db中创建的。但是,是的——你再次说对了——PHP框架(这是一个交易)如果你愿意——告诉我在CodeIgniter之后,哪个框架对我来说更容易学习?或者哪种框架更符合您的观点?好吧,如果您已经了解Codeigniter,那么一定要继续使用CI。否则,对于大型项目,我更喜欢使用Yii:这种方式可以真正扩展开发过程。