sqlhelper.class.php,SQLHelper.class.php

该博客介绍了一个名为SQLHelper的PHP类,用于连接MySQL数据库并执行DML(数据操纵语言)和DQL(数据查询语言)操作。类中包含的方法包括建立数据库连接、执行插入、更新和删除操作、查询数据并返回结果集以及实现分页查询。通过这个类,开发者可以方便地进行数据库交互和数据管理。
摘要由CSDN通过智能技术生成

//require 'FenyePage.class.php';

class SQLHelper {

private $conn;

private $host = "localhost";

private $user = "root";

private $passwd = "manager";

private $database = "test";

function __construct(){

$this->conn = mysqli_connect($this->host, $this->user, $this->passwd);

if(!$this->conn)

die("连接失败:".mysqli_error($this->conn));

mysqli_select_db($this->conn, $this->database)

or die(mysqli_error($this->conn));

mysqli_query($this->conn, "set names utf8");

}

public function exc_dml($sql){

$b = mysqli_query($this->conn, $sql)

ordie(mysqli_error($this->conn));

if(!$b){

return 0;

}else{

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

return 1;

}else{

return 2;

}

}

}

public function exc_dql($sql){

$res = mysqli_query($this->conn, $sql)

ordie(mysqli_error($this->conn));

return $res;

}

//将结果集转存到一个数组中,内存会自动释放数组所占内存

public function exc_dql2($sql){

$arr = array();

$res = mysqli_query($this->conn, $sql)

ordie(mysqli_error($this->conn));

$i = 0;

while($row = mysqli_fetch_assoc($res))

$arr[$i++] = $row;

//释放结果集

mysqli_free_result($res);

return $arr;

}

public function exc_dql2_first($sql){

$arr = array();

$res = mysqli_query($this->conn, $sql)

or die(mysqli_error($this->conn));

if($row = mysqli_fetch_assoc($res))

$arr = $row;

mysqli_free_result($res);

return $arr;

}

//将查询的分页信息保存到$fenyePage对象里

public function exc_dql_fenye($sql_pageCount, $sql_resArray,FenyePage $fenyePage){

$res = mysqli_query($this->conn, $sql_pageCount)

or die(mysqli_error($this->conn));

if($row = mysqli_fetch_row($res))

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

mysqli_free_result($res);

$fenyePage->resArray = $this->exc_dql2($sql_resArray);

}

public function connect_close(){

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

mysqli_close($this->conn);

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值