简体   繁体   中英

Error when connect to database mysql?

I have a database name : demo, table test(id, name) and using wampserver

In config.php

define('MYSQL_HOST','localhost'); // your db's host
define('MYSQL_PORT',3306);        // your db's port
define('MYSQL_USER','root'); // your db's username
define('MYSQL_PASS',''); // your db's password
define('MYSQL_NAME','demo');   // your db's database name
define('DBCHAR','utf8'); // The DB's charset

In class.database.php

  class DBConnect {
        private static $instance;
        private $connected = FALSE;

        public static function singleton() {
            if (!isset(self::$instance)) {
                $c              = __CLASS__;
                self::$instance = new $c;
            }
            return self::$instance;
        }

        public function __clone() {
            if (DB_SHOW_ERRORS === TRUE) trigger_error('We can only declare this once!', E_USER_ERROR);
            else die();
        }

        public function __construct() {
            if (version_compare(PHP_VERSION,'5.1.5','<')) die('Sorry, class only valid for PHP &gt; 5.1.5, please consider upgrading to the latest version');
            try {
                @$this->db = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_NAME,MYSQL_PORT);
                if (mysqli_connect_error()) throw new Exception('Sorry, no DB connection could be made, please run in circles while an administrator checks the system: '.mysqli_connect_error());
                else $this->connected = TRUE;
            } catch(Exception $e) {
                if (DB_SHOW_ERRORS === TRUE) trigger_error($e->getMessage(),E_USER_ERROR);
                else die();
            }
            if ($this->connected === TRUE) $this->db->set_charset(DBCHAR);
        }

        public function __destruct() {
            if ($this->connected === TRUE) $this->db->close();
        }
    }

    class DBMysql {
        private $db = null;
        public function __construct() {
            $db_connect = DBConnect::singleton();
            $this->db   = $db_connect->db;
        }

        function getList() {
            $data = array();
            $sql = 'Select id, name From test';
            $query = mysql_query($sql);
            if(!$query) {
                echo "Error: " . mysql_error();
                exit;
            }
            while($row = mysql_fetch_object($query)) {
                $data[] = $row;
            }
            return $data;
        }
    }

And final is file test.php

include('config.php');
include('class.database.php');
$mysql = new DBMysql(); 
$lists = $mysql->getList();
print_r($lists);

When i run localhost/demo/test.php is error in firebug is " Error: No database selected ", How to fix it ?

Nowhere in your DBConnect class or your DBMysql class do you call a function to set which database to work on.

I prefer using functions, so in my case it'd be mysql_select_db , but you'll of course need the OOP equivalent. Add it somewhere after establishing a successful connection.

You are mixing mysql functions with mysqli functions. You should use only one of those, either mysql or mysqli (I would suggest mysqli). Also you don't pass mysql link (which you don't even create) to mysql_query, neither you use mysqli object.

In your getList() change

 $query = mysql_query($sql);

to

$query = $this->db->query($sql); 

and

while($row = mysql_fetch_object($query)) 

to

while($row = mysqli_fetch_object($query)) 

Right.. well you got me confused with all DBConnect code. Its actually correct and uses mysqli, while in getList() you are using old mysql without explicitly selecting DB

mysql_select_db(MYSQL_NAME); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM