【php高級】mysql類

<?php
include_once('config.inc.php');
class MysqlHelper{

private $host;//數據庫地址
private $user;//數據庫帳號
private $pwd;//數據庫密碼
private $db;//需要操作的數據


//構造函數,每次實例化時調用,初始化配置
function __construct($db='mysql'){  //$db默認爲mysql
$this->db=$db;//默認爲二大系統數據庫之一,保證SQL語句都能執行
$this->host=HOST;
$this->user=USER;
$this->pwd=PWD;
	echo"


使用【mysql】擴展類調用成功!"; } //封裝需要操作的數據屬性 function setDb($value){ $this->db=$value; } function getDb(){ return $this->db; } /* Describe:切換數據庫服務器 Paramters: $host 數據庫服務器的地址 $user 數據庫服務器的帳號 $pwd 數據庫服務器的密碼 */ function ChangeDBHost($host,$user,$pwd){ $this->host=$host; $this->user=$user; $this->pwd=$pwd; } /* Describe:操作數據庫四大步 Paramters:$sql任何的SQL語句,$type=1默認引用數組,$type=2索引數組 Returns:讀取返回結果二維數組,添加/修改/刪除返回影響行數 */ function Execute($sql,$type=1){ $con=@mysql_connect($this->host,$this->user,$this->pwd) or die('連接數據庫失敗!!!!'); @mysql_select_db($this->db) or die('數據庫'.$this->db.'不存在'); //加上執行編碼 mysql_query('set names utf8');//mysql_query("set character set utf8"); if(substr_count($sql,"select ")==0) { $result=mysql_query($sql); mysql_close($con); return $result; } else{ //查詢 if(type==1){ while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){ $arr[]=$row; } } elseif($type==2){ while($row=mysqli_fetch_array($result,MYSQLI_NUM)){ $arr[]=$row; } } } echo mysql_error();//mysql的提示錯誤信息的函數! mysql_close($con); return $arr; } } ?>
發佈了83 篇原創文章 · 獲贊 12 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章