MySQLi从对象获取连接信息

时间:2016-12-25 16:09:33

标签: php object mysqli

如何从$obj = mysqli_connect($host, $user, $pass, $db)函数的实例或mysql构造函数$obj = new mysql($host, $user, $pass, $db)获取连接信息。

我有这个类,这将使MySQLi更安全,更容易使用。 但在类中我想直接导入MySQLi对象,但如果我想验证连接,我必须有连接细节。

我需要知道的课程中的所有内容都是这样的:

 <?php
  class emsqli
  {
    private $con;  // Connection will be assigned to this variable

    public  $host; // Connection (Host)
    public  $user; // Connection (Username)
    private $pass; // Connection (Password)
    public  $db;   // Connection (Database)

    private function getConnectionInformation(object $con){
      // What to do here?
      // I need the host, user, pass and db.

      // The code below does not work, but this is something I had in mind
      $this->host = $con->host;
      $this->user = $con->user;
      $this->pass = $con->pass;
      $this->db   = $con->db;

      return true;
    }
  }
?>

为何验证

<?php
    class emsql
    {
        public  $con;
        public  $host;
        public  $user;
        private $pass;
        public  $db;
        public  $connect_error;
        public  $connect_error_code;
        public  $connect_error_msg;
        public  $is_db_connected = false;
        public  $is_se_connected = false;

        public function reset(){
            $this->con = '';
            $this->host = '';
            $this->user = '';
            $this->pass = '';
            $this->db = '';
            $this->connect_error = '';
            $this->connect_error_code = '';
            $this->connect_error_msg = '';
            $this->is_db_connected = false;
            $this->is_se_connected = false;
        }



        public function import(object $mysqli){
            if(!is_object($mysqli) || !get_class($mysqli) == 'mysqli'){
                return false; // Not MySQLi object
            }
            else
            {
                if($mysqli->connect_errno){
                    return false; // Invalid object
                }

                $this->reset();
                $this->con = $mysqli;
                $this->is_se_connected = true;

                // Check if $mysqli has selected database
                if( !has_db($mysqli) ){ // This code does not work, but this is one thing I need to do
                    $this->is_db_connected = true;
                }
                return true;
            }
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

虽然可能有一些棘手的方法来获取主机,用户名和数据库,但很明显,没有办法获取密码。

此外,如果您已经有工作连接,我无法看到任何用于这些参数。