PHP编程出现的MySQL连接错误

php项目中,在循环里面再使用查询,出现了连接错误

代码:

$usr = new User(); $v = $usr->getAllDoctor(); while ($row = mysql_fetch_array($v)){ $id = $row["id"]; $docinfo = $usr->getDoctorInfoByDID($id); //医院 $hos = new hospital(); $result = $hos->queryOneHospital("id", $docinfo["hospitalid"]==""?0:$docinfo["hospitalid"]); $hname = $result == null?"":$result["name"]; //简单起见,以下内容省略 }

当运行时,一直提示数据库连接错误,原来是在循环里面new对象时出现的错误

class hospital { //private var $hospitalinfo; var $mySql = false; function __construct() { $this->mySql = DBManagerFactory::getInstance (); } }

看到每次new对象时都要重新连接数据库,导致数据库的多次重复连接

正确的代码改为如下后,程序结果正常

$usr = new User(); $hos = new hospital(); $v = $usr->getAllDoctor(); while ($row = mysql_fetch_array($v)){ $id = $row["id"]; $docinfo = $usr->getDoctorInfoByDID($id); //医院 $result = $hos->queryOneHospital("id", $docinfo["hospitalid"]==""?0:$docinfo["hospitalid"]); $hname = $result == null?"":$result["name"]; //简单起见,以下内容省略 }

 

你可能感兴趣的:(PHP编程出现的MySQL连接错误)