php mysql_select_db不能正常工作

时间:2013-03-23 16:19:19

标签: php mysql-select-db

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

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

class.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; } 
    }

的index.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>';

}

1 个答案:

答案 0 :(得分:5)

SELECT DB,您必须提供其姓名

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

正确

mysql_select_db("MyDatabaseName"); 

参考:mysql_select_db