<?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;
}
}
?>
【php高级】mysql类
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.