在php中查找意图连接函数

时间:2017-11-02 06:32:35

标签: php mysql

这是用于连接MYSQL的db.php文件。我正在使用 1.所有实例之间共享的单个实例 2.db连接配置变量 3.此方法必须是静态的,并且如果object.does尚不存在,则必须返回对象的实例。 4. clone和wakeup方法阻止了Singleton类副本的外部实例化,从而消除了重复对象的可能性。

<?php
    include('config.php');
    class db extends mysqli {


        // single instance of self shared among all instances
        private static $instance = null;


        // db connection config vars
        private $user = DBUSER;
        private $pass = DBPWD;
        private $dbName = DBNAME;
        private $dbHost = DBHOST;

        //This method must be static, and must return an instance of the object if the object
        //does not already exist.
        public static function getInstance() {
        if (!self::$instance instanceof self) {
                self::$instance = new self;
        }
            return self::$instance;
        }

        // The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
        // thus eliminating the possibility of duplicate objects.
        public function __clone() {
       trigger_error('Clone is not allowed.', E_USER_ERROR);
        }
        public function __wakeup() {
        trigger_error('Deserializing is not allowed.', E_USER_ERROR);
        }

        private function __construct() {
        parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
        if (mysqli_connect_error()) {
            exit('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
        parent::set_charset('utf-8');

       }
       public function dbquery($query)
        {
            if($this->query($query))
            {
                return true;
            }

        }
        public function get_result($query) 
        {
            $result = $this->query($query);
            if ($result->num_rows > 0){
                while ($row = $result->fetch_assoc()) 
                {
                    $rows[] = $row;     
                }
            return $rows;
            } else
            return null;


        }
    }


    ?>

这是为了安全性而使用Escape用户输入插入cording

<?php

    $Institute_Name = mysqli_real_escape_string($conn, $_REQUEST['Institute_Name']);
    $Institute_Address = mysqli_real_escape_string($conn, $_REQUEST['Institute_Address']);

?>

我需要如何从实例

中调用$conn
$conn = getInstance();

我试试这个。这不行 我需要它来插入代码

$query= "INSERT INTO `institute` ( Institute_Name, Institute_Address, ) VALUES ('$Institute_Name', '$Institute_Address')";

1 个答案:

答案 0 :(得分:1)

你应该使用

$ conn = db :: getInstance();