class?MysqlConn
{
public?$dbtype;
public?$host;
public?$dbname;
public?$user;
????public?$pwd;
function?__construct($dbtype,$dbname,$host,$user,$pwd)
{
$this->dbtype=$dbtype;
$this->host=$host;
$this->dbname=$dbname;
$this->user=$user;
$this->pwd=$pwd;
if($this->dbtype=="mysql"?||?$this->dbtype=="mssql")
{
???? ????$dsn="$this->dbtype:host=$this->host;dbname=$this->dbname";
????}
else
{
????$dsn="$this->dbtype:dbname=$this->dbname";
????}
try?
{
$conn?=?new?PDO($dsn,?$this->user,?$this->pwd);? //初始化一個PDO對象,就是創(chuàng)建了數(shù)據(jù)庫連接對象$pdo
$conn->query("set?names?utf8");
return?$conn;
????}?
catch(PDOException?$e)
{
???? ????die("Error!:?"?.?$e->getMessage()?.?"<br/>");
????}????
}?
}
$conn=new?MysqlConn("mysql","dzxx_db","localhost","admin","123");//數(shù)據(jù)庫連接類實(shí)例化,執(zhí)行連接操作,返回連接標(biāo)識
3 回答

天使之翼死神之鐮
TA貢獻(xiàn)81條經(jīng)驗(yàn) 獲得超102個贊
$dsn的值有問題,正確的應(yīng)該分別是?$dsn=$this->dbtype.":host=".$this->host.";dbname=".$this->dbname;和$dsn=$this->dbtype.":dbname=".$this->dbname;
點(diǎn)擊展開后面1條

錯過了年華
TA貢獻(xiàn)56條經(jīng)驗(yàn) 獲得超22個贊
class?ConnDB{ var?$dbtype; var?$host; ????var?$user; ????var?$pwd; ????var?$dbname; ???? //構(gòu)造方法 ????function?ConnDB($dbtype,$host,$user,$pwd,$dbname){ $this->dbtype=$dbtype; ????????? $this->host=$host; $this->user=$user; $this->pwd=$pwd; $this->dbname=$dbname; } ????//實(shí)現(xiàn)數(shù)據(jù)庫的連接并返回連接對象 ????function?GetConnId(){ ????? ???? if($this->dbtype=="mysql"?||?$this->dbtype=="mssql"){ ???? $dsn="$this->dbtype:host=$this->host;dbname=$this->dbname"; }else{ $dsn="$this->dbtype:dbname=$this->dbname"; }???? try?{ ???? $conn?=?new?PDO($dsn,?$this->user,?$this->pwd);? //初始化一個PDO對象,就是創(chuàng)建了數(shù)據(jù)庫連接對象$pdo $conn->query("set?names?utf8"); ???? return?$conn; }?catch?(PDOException?$e)?{ ???? die?("Error!:?"?.?$e->getMessage()?.?"<br/>"); } ????? ????} }
上面這個是原本的類,可沒見需要像你那樣寫啊
添加回答
舉報(bào)
0/150
提交
取消