所以我想做一个MySQL数据库类,我想保持我的数据库选择在一个单独的方法从构造函数。出于某种原因,它的setDb()函数不想工作。Mysql数据库类 - 连接故障
class mysql
{
public function __construct($server,$user,$pass)
{
if(!$this->mysql_connection = mysql_connect($server,$user,$pass))
print 'Could not connect to MySQL';
}
public function setDb($dbname)
{
$this->database = $dbname;
if(!mysql_select_db($this->database,$this->mysql_connection))
$this->database = '';
print 'Could not connect to the MySQL database';
return false;
return true;
}
private $database;
private $mysql_connection;
}
,而不是打印并返回你可能想尝试死(),以确保你是不是报表缺少错误信息。另外,你怎么打电话给你的MySQL类? – 2009-10-19 18:13:40
die(mysql_error())应该吐出错误细节 – Tim 2009-10-19 18:21:17
您是否考虑过使用/学习现有的数据库访问层和/或抽象 - 例如http://docs.php.net/pdo,http://adodb.sourceforge.net,http://www.doctrine-project.org,...很多很多 - 在尝试自己的之前? – VolkerK 2009-10-19 18:33:53