php mysql\u select\u数据库不工作
我创建了一个名为“class.admin.php”的类,它执行一些检查。 我有一个调用类的文件,该类在它尝试选择db的地方运行良好 当我运行php mysql\u select\u数据库不工作,php,mysql-select-db,Php,Mysql Select Db,我创建了一个名为“class.admin.php”的类,它执行一些检查。 我有一个调用类的文件,该类在它尝试选择db的地方运行良好 当我运行mysql\u select\u db()或die时,我得到错误“未选择任何数据库” class.admin.php class admin { ### Function that check for the connect file (if it exists) public function checkConnector() {
mysql\u select\u db()或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>';
}
$admin=new admin();
#检查连接文件是否存在
如果($admin->checkConnector()==true){
#检查与MYSQL服务器的连接
如果($admin->checkConnection()==true){
###检查数据库的选择
如果($admin->checkDB()==true){
打印“选择数据库很好”;
}否则{
打印“选择的数据库不起作用”;
}
}否则{
打印“对不起,无法连接到MYSQL。”;
};
}否则{
打印“很抱歉,连接文件不存在。请相应安装。”;
}
要选择aDB
,必须提供其名称
mysql_select_db(); // wrong, which database to select?
正确的是
mysql_select_db("MyDatabaseName");
参考:BTW:if(foo()){return true;}else{return false;}
。。。请将其缩写为sanereturn foo()代码>。需要一个字符串(数据库名称)作为第一个参数。我不理解这个问题的缺点。那么为什么会出现堆栈溢出?@HPM它肯定不是用来调试单个用户的一次性错误的。它应该是编程中反复出现的问题的集合。这不是他们中的一个,我现在明白了!谢谢@deceze。我已经将class.admin修改为:if(mysql_select_db(NAME)){return true;}否则{return false;}NAME是定义的db的名称。仍然没有乐趣。但你发布的代码中并非如此。在这里,您没有给出要选择的数据库的任何名称。更新后错误是否仍然存在?检查函数中的常量值是否正确I修改了类函数,以便在连接文件中使用已定义变量的名称。我没有添加它,因为检查连接的方法有效。该方法现在的内容是:if(mysql_select_db(NAME)){return true;}else{return false;}但仍然没有joy.NAME是否正确地传递给方法,如果您回显它是否显示正确的值?是的,回显NAME;//打印新VODB