连接数据库mysql时出错?

时间:2012-03-21 10:20:03

标签: php

我有一个数据库名称:demo,table test(id,name)和wampserver

在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

在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;
        }
    }

最后是文件test.php

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

当我运行localhost / demo / test.php时,firebug中的错误是“Error: No database selected”,如何解决?

3 个答案:

答案 0 :(得分:0)

您的DBConnect类或DBMysql类中没有任何地方调用函数来设置要处理的数据库。

我更喜欢使用函数,所以在我的情况下它是mysql_select_db,但你当然需要等效的OOP。在建立成功连接后将其添加到某处。

答案 1 :(得分:0)

您正在将mysql函数与mysqli函数混合使用。你应该只使用其中一个,mysql或mysqli(我建议使用mysqli)。 另外你不要将mysql链接(你甚至没有创建)传递给mysql_query,也不要使用mysqli对象。

getList()更改

 $query = mysql_query($sql);

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

while($row = mysql_fetch_object($query)) 

while($row = mysqli_fetch_object($query)) 

答案 2 :(得分:0)

对..你让我对所有DBConnect代码感到困惑。它实际上是正确的并使用mysqli,而在getList()中你使用旧的mysql而没有明确选择DB

mysql_select_db(MYSQL_NAME); 
相关问题