PHP 建立 MySQLi 封裝類別(class)+interface

PHP 建立 MySQLi 封裝類別(class)+interface

PHP 建立 MySQLi 封裝類別(class)+interface

 

<?php

/*

 MySQLi的指令集

 http://www.w3schools.com/php/php_ref_mysqli.asp

 */

interface action { 

        public function connecSQL();  //connection to the MySQL server

        public function set_charset($charset1=”utf8″);//Sets the default client character set

        public function selectQuery($Query);

        public function closeSQL();//Closes a previously opened database connection

}

class CMySQL implements action {

        private $m_MySQLConn = null;

        private $m_StrServerName = null;

        private $m_StrUserName = null;

        private $m_StrPassWord = null;

        private $m_StrDBName = null;

        private $m_blnDebug = null;

        function __construct($hostname,$username,$password,$DBName,$blnDebug) {

                $this->m_StrServerName = $hostname;

                $this->m_StrUserName = $username;

                $this->m_StrPassWord = $password;

                $this->m_StrDBName = $DBName;

                $this->m_blnDebug = $blnDebug;

                if($this->m_blnDebug==true)

                {

                        echo “CMySQL_construct runed.\n”;

                }      

        }

        function __destruct() {

                $this->closeSQL();

                if($this->m_blnDebug==true)

                {

                        echo “CMySQL_destruct runed.\n”;

                }

        }

        function connecSQL() {

                if($this->m_MySQLConn==null)

                { 

                        //echo “create m_MySQLConn”;

                        $this->m_MySQLConn = new mysqli($this->m_StrServerName, $this->m_StrUserName, $this->m_StrPassWord, $this->m_StrDBName);

                        if ($this->m_MySQLConn->connect_error) {

                            die(“Connection failed: ” . $this->m_MySQLConn->connect_error);

                        }

                        if($this->m_blnDebug==true)

                        {

                                echo “connecSQL runed.\n”;

                        }

                }

                else

                {

                        if($this->m_blnDebug==true)

                        {

                                echo “Error: The Obj is established.\n”;

                        }

                }

    }

        function set_charset($charset1=”utf8″) {

                mysqli_set_charset($this->m_MySQLConn,$charset1);//設定編碼原則

                if($this->m_blnDebug==true)

                {

                                echo “set_charset runed.\n”;

                }

        }

        function selectQuery($Query) {

                $result=$this->m_MySQLConn->query($Query);

            if($this->m_blnDebug==true)

                {

                                echo “selectQuery runed.\n”;

                }

                return $result;

        }

    function closeSQL() {

            if($this->m_MySQLConn!=null)

            {

                    mysqli_close($this->m_MySQLConn);

                    $this->m_MySQLConn=null;

                    if($this->m_blnDebug==true)

                        {

                                echo “MySQL Obj closed.\n”;

                        }

            }

    }

}

?>

 

 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *