一共两个页面,database.inc.php和test.php
看代码:database.inc.php
复制内容到剪贴板
代码:
class mysql {
var $usepconn = '0';//是否启用长久连接,0为启用,1为不启用
var $hostname = '';
var $hostuser = '';
var $hostpass = '';
var $hostdb = '';
var $dblink = 0;
var $dbselect = 0;
function connect() {
/*if($this->usepconn == "1") {
$this->dblink = @mysql_connect($this->hostname,$this->hostuser,$this->hostpass) or die ("MySQL Connection Error");
}elseif($this->usepconn == "0"){
$this->dblink = @mysql_pconnect($this->hostname,$this->hostuser,$this->hostpass) or die ("MySQL Pconnection Error");
}*/
$this->dblink = @mysql_connect($this->hostname,$this->hostuser,$this->hostpass) or die ("MySQL Connection Error");
}
function dbselect() {
$this->dbselect = @mysql_select_db($hostdb,$this->dblink) or die ("Could not found the $this->hostdb in your MySQL Databases!");
}
}
?>test.php
复制内容到剪贴板
代码:
$filename = 'database.inc.php';
if(file_exists("$filename")) {
require "$filename";
}else{
echo 'Not Found The '.$filename.' File!';
exit;
}
##################################################
$newmysql = new mysql;##初始化数据库类 #
##################################################
$newmysql->hostname = 'localhost'; #
$newmysql->hostuser = 'root'; #
$newmysql->hostpass = 'kawtingting'; #
$newmysql->hostdb = 'mysql'; #
##################################################
$newmysql->connect();
$newmysql->dbselect();
?>结果提示错误:
$this->dbselect = @mysql_select_db($hostdb,$this->dblink) or die ("Could not found the $this->hostdb in your MySQL Databases!");