未选择数据库 php 5.3

发布于 2025-01-08 21:56:18 字数 2774 浏览 0 评论 0原文

我有一种在 php 5.2 中可以正常工作的连接,但现在未更新到版本 5.3,它会生成错误:“未选择数据库”。这是我的脚本:

config.php :

$host = 'localhost'
$user = 'root'
$password = ''
$db = 'mydb'

Conf.class.php :

class Conf{

    private $_userdb;    
    private $_passdb;    
    private $_hostdb;    
    private $_db;     
    static $_instance;

    private function __construct(){
               require 'config.php';  
           $this->_userdb=$user;       
           $this->_passdb=$password;       
           $this->_hostdb=$host;       
           $this->_db=$db;
    } 

    private function __clone(){ }

    public static function getInstance(){
            if (!(self::$_instance instanceof self)){          
                self::$_instance=new self();
            }       
            return self::$_instance;
    }

    public function getUserDB(){
            $var=$this->_userdb;       
            return $var;    
    }

    public function getHostDB(){
            $var=$this->_hostdb;       
            return $var;    
    }     
    public function getPassDB(){
            $var=$this->_passdb;       
            return $var;    
    } 

    public function getDB(){
            $var=$this->_db;       
            return $var;    
    } 
}

Db.class.php :

class Db {
     private $server;    
     private $user;    
     private $password;    
     private $data_base;  
     private $link;
     private $result;    
     static $_instance; 


     private function __construct() {
            $this->setConnection();       
        $this->connect();
        $this->result = null;
     }

     private function setConnection() {
            $conf = Conf::getInstance();       
        $this->server = $conf->getHostDB();       
        $this->data_base = $conf->getDB();       
        $this->user = $conf->getUserDB();      
        $this->password = $conf->getPassDB();
     }

     private function __clone(){ }

     public static function getInstance() {

            if (!(self::$_instance instanceof self)){
                      self::$_instance=new self();       
            }          return self::$_instance;

     } 


         private function connect() {

            $link=mysql_connect($this->server, $this->user, $this->password); 
            if ($link){      
                mysql_select_db($this->data_base,$link);
            } 
            if (!$link){        
                die('Can not connect');
            }else{
                $this->link = $link;    
            } 

     }
}

显然是Db.class类,没有操作函数getInstance(),因为没有从Conf.class获取数据。

我需要更改 Db.class 和 Conf.class 中的某些内容吗?

I have a kind of connection that worked correctly in php 5.2, but now un updating to version 5.3 and it generates the error: "Database not selected". This is my script:

config.php :

$host = 'localhost'
$user = 'root'
$password = ''
$db = 'mydb'

Conf.class.php :

class Conf{

    private $_userdb;    
    private $_passdb;    
    private $_hostdb;    
    private $_db;     
    static $_instance;

    private function __construct(){
               require 'config.php';  
           $this->_userdb=$user;       
           $this->_passdb=$password;       
           $this->_hostdb=$host;       
           $this->_db=$db;
    } 

    private function __clone(){ }

    public static function getInstance(){
            if (!(self::$_instance instanceof self)){          
                self::$_instance=new self();
            }       
            return self::$_instance;
    }

    public function getUserDB(){
            $var=$this->_userdb;       
            return $var;    
    }

    public function getHostDB(){
            $var=$this->_hostdb;       
            return $var;    
    }     
    public function getPassDB(){
            $var=$this->_passdb;       
            return $var;    
    } 

    public function getDB(){
            $var=$this->_db;       
            return $var;    
    } 
}

Db.class.php :

class Db {
     private $server;    
     private $user;    
     private $password;    
     private $data_base;  
     private $link;
     private $result;    
     static $_instance; 


     private function __construct() {
            $this->setConnection();       
        $this->connect();
        $this->result = null;
     }

     private function setConnection() {
            $conf = Conf::getInstance();       
        $this->server = $conf->getHostDB();       
        $this->data_base = $conf->getDB();       
        $this->user = $conf->getUserDB();      
        $this->password = $conf->getPassDB();
     }

     private function __clone(){ }

     public static function getInstance() {

            if (!(self::$_instance instanceof self)){
                      self::$_instance=new self();       
            }          return self::$_instance;

     } 


         private function connect() {

            $link=mysql_connect($this->server, $this->user, $this->password); 
            if ($link){      
                mysql_select_db($this->data_base,$link);
            } 
            if (!$link){        
                die('Can not connect');
            }else{
                $this->link = $link;    
            } 

     }
}

Apparently Db.class class, does not operate the function getInstance () because no gets the data from Conf.class.

I need to change something in Db.class and Conf.class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞取 2025-01-15 21:56:18

可能是复制/粘贴问题,但您的数据库构造调用 setConnection,而您的方法名称是 setConexion。您还在配置文件中省略了分号。

否则,你的代码对我来说运行得很好。可能会考虑添加一个 error_reporting(E_ALL);并从命令行进行测试。您可能遇到包含文件路径问题,但您只是没有看到错误。

Probably a copy/paste issue but your db construct calls setConnection while your method name is setConexion. You also left out the semi colons in your config file.

Otherwise, your code ran fine for me. Might consider adding an error_reporting(E_ALL); and testing from the command line. You probably have an include file path issue and you are just not seeing the errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文