And*_*ebb -2 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; }
}
Run Code Online (Sandbox Code Playgroud)
的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>';
}
Run Code Online (Sandbox Code Playgroud)
要SELECTa DB,你必须提供它的名字
mysql_select_db(); // wrong, which database to select?
Run Code Online (Sandbox Code Playgroud)
正确的是
mysql_select_db("MyDatabaseName");
Run Code Online (Sandbox Code Playgroud)