-2

我创建了一个名为“class.admin.php”的类,它进行了一些检查。我有一个调用类的文件,该类在尝试选择数据库时工作正常。

当我运行时,mysql_select_db() or die我收到错误“未选择数据库”。

类.admin.php

class admin {

    ### Function that check for the connect file (if it exists)
    public function checkConnector() {
        if(file_exists(CONN)) { return true; } else { return false; }
    }

    ### Check connection to MYSQL
    public function checkConnection() {
        global $cn; if(mysql_connect()) { return true; } else { return false; }
    }

    ### Check connection to database
    public function checkDB() { 
        global $db; if(mysql_select_db()) { return true; } else { return false; } 
    }

索引.php

$admin = new admin();

# Check the connect file exists
if($admin->checkConnector() === true) {

    # Check connection to MYSQL server
    if($admin->checkConnection()  === true) {

        ### Check selection of DB
        if($admin->checkDB() === true) {

            print 'Selection of database is fine.';

        } else {

            print 'Selection of database is not working.';

        }

    } else {

        print '<p>I\'m sorry, could not connect to MYSQL.</p>';

    };

} else {

    print '<p>I\'m sorry the connection file does not exist. Please install accordingly.</p>';

}
4

1 回答 1

5

对于SELECTa DB,您必须提供其名称

mysql_select_db();   // wrong, which database to select?

正确的是

mysql_select_db("MyDatabaseName"); 

参考:mysql_select_db

于 2013-03-23T16:21:55.587 回答