mysql資料庫類中出現的問題

來源:互聯網
上載者:User
mysql

這是我的mysql類,繼承至db抽象類別,其中db的抽象方法在mysql中都以實現,
db類:
abstract class db{
//串連資料庫
abstract public function connect($h,$u,$p);
//發送查詢
abstract public function query($sql);
//查詢多行資料
abstract public function getAll($sql);
//查詢單行資料
abstract public function getOne($sql);

//查詢單個資料
abstract public function getRow($sql);
abstract public function autoExecute($arr,$table,$mode='insert ',$where='1 limit 1');
}
class mysql extends db{
private static $ins=null;
private $conn=null;
private $conf=array();
//讀取資料庫的配置資訊
protected function __construct(){
$this->conf=conf::getIns();
$this->select_db($this->conf->db);
$this->connect($this->conf->host,$this->conf->user,$this->conf->pwd);//顯示是空
$this->setChar($this->conf->char);
}
public function __destruct(){

}
//使用單例模式,只允許new一次
public static function getIns(){
if(self::$ins===null){
self::$ins=new self();
}
return self::$ins;
}
//串連,串連失敗時,拋出異常
public function connect($h,$u,$p){
//沒走到這一步
$this->conn=mysql_connect($h,$u,$p);
}
protected function select_db($db){
$sql='use '.$db;
$this->query($sql);
}

protected function setChar($char){
$sql='set names '.$char;
return $this->query($sql);
}
//發送資料
public function query($sql){
/*if($this->conf->debug){
log::write($sql);
}*/
/* var_dump($sql);
exit;*/
$rs=mysql_query($sql,$this->conn);
/*if(!rs){
log::write($this->error());
}*/
if(!$rs){
echo '失敗
';
var_dump($this->conn);
echo '
';
var_dump($this->conf);


}
return $rs;
}
//自動進行計算
public function autoExecute($arr,$table,$mode='insert ',$where='1 limit 1'){
if(!is_array($arr)){
return false;
}//更新表中的資料
if($mode=='update'){
$sql='update '.$table.'set';
foreach($arr as $k=>$v){
$sql.=$k."='".$v."',";
}
$sql=rtrim($sql,',');
$sql.=$where;
return $this->query($sql);
}
$sql='insert into '.$table.'('.implode(',',array_keys($arr)).')';
$sql.='values(\'';
$sql.=implode("','",array_values($arr));
$sql.='\')';
return $this->query($sql);
}
//取出表中所有合格多行資料
public function getAll($sql){
$rs=$this->query($sql);
$list=array();
while($row=mysql_fetch_assoc($rs)){
$list[]=$row;
}
return $list;
}
//取出合格一行資料
public function getRow($sql){
$rs=$this->query($sql);
return mysql_fetch_assoc($rs);
}
//取出一個資料
public function getOne($sql){
$rs=$this->query($sql);
$row=mysql_fetch_assoc($rs);
return $row[0];
}
//取出影響的資料
public function affected_rows(){
return mysql_affected_rows($this->conn);
}
//插入一個id
public function insert_id(){
return mysql_insert_id($this->conn);
}
}

為什麼會出現:Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource
排查了很多錯誤,發現程程式沒有走到
public function connect($h,$u,$p){
//沒走到這一步
$this->conn=mysql_connect($h,$u,$p);
}
想不出什麼原因


回複討論(解決方案)

//讀取資料庫的配置資訊
protected function __construct(){

你把建構函式指定成 保護模式
如何能被執行?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.