dongshi3361 2014-10-19 09:58
浏览 6
已采纳

无法在子类中重置我的数据库连接

I have 3 classes: This is main DB class - if will be use in other two classes.

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");
    }
}

And this is my crud class:

 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]);
}   

} And here are my third class - it load data from selected database using connection of CRUD class:

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().

I real didnt know why $crud->resetPreventDB() dont working - but I real wont this code, because this bug stop developing of all my startup. Will very Thanks for you understanding & help) Thanks.

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-10-19 13:27
    关注

    Mate, you are calling the function getData() , but in this function there is no reference to resetPreventDB() function.

    Why the database should be changed?

    Maybe you want to call the function getList() instead of getData() ?

    BTW: It will look better if your code do not use globals, like that maybe:

    public function getList(){ $q = $this->getData(); $this->resetPreventDB($this->db); return $q; }

    As a general advice - consider using any of the PHP frameworks around.
    All similar issues are fixed long ago and are extensively tested by many developers.
    After the initial learning curve, you will be able to develop at least 10 times faster.
    If you need advise for specific framework - just ask.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿