我创建了一个名为'class.admin.php'的类,它做了一些检查。我有一个文件,调用类的工作很好,直到它试图选择db.
当我运行mysql_select_db() or die
时,我得到错误'No database selected'。
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>';
}
对于SELECT
a DB
,您必须提供其名称
mysql_select_db(); // wrong, which database to select?
正确mysql_select_db("MyDatabaseName");
参考:mysql_select_db