开发者

mysql database connection problem in php

final class MySQL {
 private $connection;

 public function __construct($hostname, $username, $password, $database) {
  if (!$this->connection = mysql_connect($hostname开发者_C百科, $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);
   }

Error : Could not connect to database...


Let MySQL tell you more about the error, see 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);
    }

But as mentioned earlier: Don't build another (sorry for being blunt) crappy MySQL class. Use an existing (one of the better ones) class/library, gain experience, then -if you still want and are committed- try again to build a really good one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜