首页 > 代码库 > PHP历程(PHP与MYSQL数据库之间连接、创建和关闭)

PHP历程(PHP与MYSQL数据库之间连接、创建和关闭)

<?phpdefine(‘WXLEVELS_DB_HOST‘,‘127.0.0.1‘);    //服务器define(‘WXLEVELS_DB_USER‘,‘root‘);         //数据库用户名define(‘WXLEVELS_DB_PASS‘,‘123456‘);       //数据库密码define(‘WXLEVELS_DB_NAME‘,‘PvUv‘);   //默认数据库define(‘DB_CHARSET‘,‘utf8‘);               //数据库字符集define(‘TIMEZONE‘,"PRC");                  //时区设置class dataConn{    public function dataConn($host=WXLEVELS_DB_HOST ,$username=WXLEVELS_DB_USER,$password=WXLEVELS_DB_PASS,$dbname=WXLEVELS_DB_NAME)    {        $this->host = $host;        $this->username = $username;        $this->password = $password;        $this->dbname = $dbname;    }    public function open()    {        $this->conn = mysql_connect($this->host,$this->username,$this->password) or die (‘数据库连接失败!</br>错误原因:‘.mysql_error());        mysql_select_db($this->dbname) or die(‘数据库选定失败!</br>错误原因:‘.mysql_error());        mysql_query("SET CHARACTER SET utf8");    }    public function close()    {        mysql_close($this->conn);    }}