final class MySQL {
private $connection;
public function __construct($hostname, $username, $password, $database) {
if (!$this->connection = mysql_connect($hostname, $username, $password)) {
exit('Error: Could not make a database connection using ' . $username . '@' . $hostname);
}
if (!mysql_select_db($database, $this->connection)) {
exit('Error: Could not connect to database ' . $database);
}
mysql_query("SET NAMES 'utf8'", $this->connection);
mysql_query("SET CHARACTER SET utf8", $this->connection);
mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection);
mysql_query("SET SQL_MODE = ''", $this->connection);
}
错误:无法连接到数据库......
答案 0 :(得分:1)
让MySQL告诉您有关错误的更多信息,请参阅http://docs.php.net/mysql_error
define('DEBUGOUTPUT', 1);
final class MySQL {
private $connection;
public function __construct($hostname, $username, $password, $database) {
if (!$this->connection = mysql_connect($hostname, $username, $password)) {
if ( defined('DEBUGOUTPUT') && DEBUGOUTPUT ) {
echo __METHOD__, ': ', mysql_error(), "\n";
}
exit('Error: Could not make a database connection using ' . $username . '@' . $hostname);
}
但正如前面所提到的:不要建立另一个(抱歉是直率)糟糕的MySQL类。使用现有的(一个更好的)类/库,获得经验,然后 - 如果你仍然想要并且承诺 - 再次尝试建立一个非常好的。