我有两个几乎完全相同的类,它们独立工作但不在两个同一页面上。
似乎给出了奇怪的输出错误,将两个类组合在一起。
我在这里犯了错误吗?
$db = new query("localhost","xxxxxx","xxxxxx","xxxxxx");
class query extends hush {
public function __construct($host,$user,$pass,$dbname) {
mysql_connect($host,$user,$pass)
or die(mysql_error());
mysql_select_db($dbname)
or die(mysql_error());
}
public function qry($sql) {
// return mysql_query($sql);
$query = mysql_query($sql) or die(mysql_error());
return $query;
}
}
$dbx = new queryx("localhost","xxxxxx","xxxxxx","xxxxxx");
class queryx extends hush {
public function __construct($host2,$user2,$pass2,$dbname2) {
mysql_connect($host2,$user2,$pass2)
or die(mysql_error());
mysql_select_db($dbname2)
or die(mysql_error());
}
public function qry2($sql2) {
// return mysql_query($sql);
$query2 = mysql_query($sql2) or die(mysql_error());
echo $query2;
}
}
答案 0 :(得分:1)
这是使用mysqli的程序示例。但是,由于您正在使用类,我认为您应该尝试使用对象。
class query extends hush {
protected $db;
public function __construct($host,$user,$pass,$dbname) {
$this->db = mysqli_connect($host,$user,$pass,$dbname)
}
public function qry($sql) {
return mysqli_query($this->db, $sql) or die(mysqli_error());
}
}
class queryx extends hush {
protected $db;
public function __construct($host,$user,$pass,$dbname) {
$this->db = mysqli_connect($host,$user,$pass,$dbname)
}
public function qry($sql) {
return mysqli_query($this->db, $sql) or die(mysqli_error());
}
}
注意我已使用类属性作为链接标识符($this->db
)。这样可以防止多个连接发生冲突。