php连接数据库封装类,一个php操作数据库的封装类,超实用. | 学步园

详情请点击 断桥残雪

class SqlHelper{

private $dbname="";

private $userName="";

public  $password="";

private $conn;

private $host="";

/**

* @author:lixiuran

* 初始化数据库的配置信息

* Enter description here ...

*/

public function configInit(){

$filename="../config/db_info.ini";

$db_infos=parse_ini_file($filename);

for($i=0;$i

$this->host= $db_infos["host"];

$this->userName=$db_infos["user_name"];

$this->dbname=$db_infos["db_name"];

$this->password=$db_infos["password"];

}

}

/*

* 该类的构造放发,也可用__construct

* 初始化连接,选择的数据库,以及编码方式。

*/

public function  SqlHelper(){

$this->configInit();

$this->conn=mysql_connect($this->host,$this->userName,$this->password);

if(!$this->conn){

echo "数据库连接失败".mysql_error();

}

$db=mysql_select_db($this->dbname,$this->conn);

if(!$db){

echo "数据库选择失败".mysql_error();

}

mysql_query("set character set gbk") or die("编码设置错误".mysql_error());

mysql_query("SET NAMES 'GBK'") or die("编码设置错误".mysql_error());

}

/*

* 执行查询语句,并返回一个二维数组

*/

public function execute_dql($sql){

$arr=array();

$result=mysql_query($sql,$this->conn) or die(mysql_error());

while($row=mysql_fetch_assoc($result)){

$arr[]=$row;

}

mysql_free_result($result);

return $arr;

}

/**

* $sql_fenye_page:分页查询的sql语句

* $sql_total_count:查询表的总记录数。

*

* Enter description here ...

* @param unknown_type $sql_fenye_page

* @param unknown_type $sql_total_count

* @param unknown_type $fenye

*/

public function execute_dql_fenye($sql_fenye_page,$sql_total_count,$fenye){

$result=mysql_query($sql_fenye_page,$this->conn) or die(mysql_error());

$arr=array();

while($row=mysql_fetch_assoc($result)){

$arr[]=$row;

}

mysql_free_result($result);

$fenye->res_arr=$arr;

//处理总记录数及页数

$result2=mysql_query($sql_total_count) or die(mysql_error());

if($row=mysql_fetch_row($result2)){

$fenye->pageCount=ceil($row[0]/$fenye->pageSize);

$fenye->rowCount=$row[0];

}

//导航条*************************************************************************

$nav="";

$pageWhole=5;

$start=floor(($fenye->pageNow-1)/$pageWhole)*$pageWhole+1;

$index=$start;

$nav.="goUrl}?pageNow=1>首页 ";

if($fenye->pageNow>1){

$nav.="goUrl}?pageNow=".($fenye->pageNow-1).">上一页 ";

}

//循环打印草链接

if($fenye->pageNow>$pageWhole){

$nav.="goUrl}?pageNow=".($start-1)."> <<";

}

for(;$start

if($start<=$fenye->pageCount){

$nav.=" goUrl}?pageNow=".$start.">[".$start."]";

}

}

if($fenye->pageNowpageCount-$pageWhole)){

$nav.=" goUrl}?pageNow=".$start."> >>";

}

if($fenye->pageNowpageCount){

$nav.="goUrl}?pageNow=".($fenye->pageNow+1)."> 下一页";

}

$nav.="goUrl}?pageNow=$fenye->pageCount> 尾页";

$nav.="当前$fenye->pageNow/{$fenye->pageCount}页|共{$fenye->rowCount}条";

$nav.= "

";

$nav.="";

$nav.=" ";

$nav.="";

$fenye->navigator=$nav;

//*************************************************************************

mysql_free_result($result2);

return $fenye;

}

/*

* 执行 crud 操作,并返回状态玛。

*/

public function execute_dml($sql){

$b=mysql_query($sql,$this->conn);

if(!$b){

return 0;

}else{

if(mysql_affected_rows($this->conn)>0){

return 1;//表示执行成功

}else{

return 2;//表示没有行数受到影响。

}

}

}

/*

* 关闭连接,释放资源。

*/

public function close_connect(){

if(!empty($this->conn)){

mysql_close($this->conn);

}

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值