Mysql
 sql >> база данни >  >> RDS >> Mysql

Най-добрият начин за създаване на конфигурационен файл (config.php) php

1) създайте config.php

define('DBUSER','username');
   define('DBPWD','password');
   define('DBHOST','localhost');
   define('DBNAME','database name');

2) db.php

 <?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){
            $row = $result->fetch_assoc();
            return $row;
            } else
            return null;


        }
    }


    ?>

3) използва

 require 'db.php';
    $query="select * from tbl_session";
    $sockets = db::getInstance()->get_result($query);

или всяка друга заявка

$query="insert into `tbl_chats` (coloum_name) values('".$val."')";
$wisherID = db::getInstance()->dbquery($query);


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Върнете null за date_format, когато входът е null в mysql

  2. Как трябва да съхранявам GUID в MySQL таблици?

  3. MySQL IFNULL N/A добив елемент не може да бъде намерен в колекцията Грешка

  4. Колко важни са справочните таблици?

  5. Как да изтрия от select в MySQL?