当前位置:网站首页>PHP连接mysql数据库,数据库连接静态工具类,简化连接。
PHP连接mysql数据库,数据库连接静态工具类,简化连接。
2022-07-26 03:19:00 【清风亦思雨】
写这个PHP连接数据库静态工具类之前,是由于当时学习C#的asp.net。老师编写了一个连接数据库的静态工具类,发现使用此类,每次只需要写sql语句,而不再需要进行连接创建,关闭等等。方便其他操作,节约时间。
转眼一年半,如今php课程需要写一个小项目,于是第一件事就是编写一个连接数据库的静态工具类,php和asp.net的语法极其的相似。
在使用此类之前,可以普及两点知识:
一、PHP中使用静态的调用,不同于其他编程语言,它的静态调用为:
类名::$静态属性
类名::静态方法()
而Java、C#等编程语言都是通过:
类名.静态属性
类名.静态方法()
二:静态方法的优点:
(1)在代码的任何地方都可以用,不需要实例对象就能访问静态属性或方法;
(2)类的每个实例都可以访问类中定义的静态属性,可以利用静态属性来设置值,该值可以被类的所有对象使用,方便控制数据库参数,连接对象的创建与关闭;
DBHelper.php 静态工具类
<?php
/**该PHP文件为了连接数据库方便
* 为连接数据库提供静态方法
*/
header("Content-type: text/html;charset=utf-8");
class DBHelper {
static $sqlName = "127.0.0.1";
static $userName = "root";
static $passWord = "123456";
static $dbName = "test";
//连接数据库
static function dbConn() {
$conn = mysqli_connect(self::$sqlName, self::$userName,
self::$passWord, self::$dbName);
if ($conn == false) {
echo "<script>alert('数据库连接失败')</script>";
}
//设置连接对象编码
mysqli_query($conn, "set names utf8");
return $conn;
}
//查询数据
//param1:查询类型,param2:执行语句
//param1参数类型: 0单个数组,1多个数组。
static function select($state, $mysql):array {
$conn = self::dbConn(); //获得连接对象
$sql = $mysql;
$query = mysqli_query($conn, $sql);
if ($state == 1) {
$result = mysqli_fetch_all($query,MYSQLI_ASSOC);
} else {
$result = mysqli_fetch_assoc($query);
}
// 释放结果集
mysqli_free_result($query);
self::dbClose($conn); //释放连接对象
if ($result == null) { //如果结果为空,则返回空数据集
return array();
}
return $result;
}
//增加、删除、修改数据
//param1:执行类型;param2:执行语句
//param1参数类型:1增加,2修改,3删除。
static function sqlHelper($state, $mysql):int {
$conn = self::dbConn(); //获得连接对象
$sql = $mysql;
$query = mysqli_query($conn, $sql);
//判断状态,做出相应提示。
//$sts = $state==3?"删除":($state==2?"修改":($state==1?"增加":$state));
self::dbClose($conn); //释放连接对象
if ($query){
return 1; //有内容变化
//return $sts."成功";
}
else{
return 0; //无内容变化
//return $sts."失败";
}
}
//关闭连接
static function dbClose($conn) {
$conn ->Close();
}
}
?>
Test.php 用来测试数据
//查询单个数据
$select = DBHelper ::select(0, "select * from XXX where xx = '$xx'");
//查询多个数据
$selectAll = DBHelper ::select(1,"select * from XXX");
//添加数据
$insert = DBHelper ::sqlHelper(1, "insert into XXX (xx,xx,xx) values ('$xx','$xx','$xx')");
//修改数据
$update = DBHelper ::sqlHelper(2, "update XXX set xx = '$xx',xx = '$xx',xx = '$xx', where xx = '$xx'");
//删除数据
$delete = DBHelper ::sqlHelper(3, "delete from XXX where xx = '$xx'");前两条测试数据,都是查询,设置的返回参数分别为mysqli_fetch_assoc、mysqli_fetch_all,这两个参数返回的都是array,只不过里面有单个数据或多个数据。
mysqli_fetch_assoc:

mysqli_fetch_all:

后三条对应的是增删改,返回的int类型参数,可以判断执行是否成功。
边栏推荐
- ES6 set and map
- 【TensorFlow&PyTorch】图像数据增强API
- 【虚拟化】查看vCenter和ESXi主机的Log日志文件
- Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
- 78. 子集
- [noip2001 popularization group] the problem of maximum common divisor and minimum common multiple
- 实现一个方法,找出数组中的第k大和第m大的数字相加之和
- c语言指针基本知识要点总结(一)
- PXE efficient batch network installation
- 爆肝出了4W字的Redis面试教程
猜你喜欢
随机推荐
Offline data warehouse from 0 to 1-stage II software installation
[STL]优先级队列priority_queue
easyExcel设置行隐藏,解决setHidden(true)失效问题
ue4如何进行静态渲染?5个步骤生成静态渲染
如何正确计算 Kubernetes 容器 CPU 使用率
QT笔记——Q_Q 和Q_D 学习
Managing databases in a hybrid cloud: eight key considerations
canvas——矩形的绘制——柱状图的制作
tf.constant用法
实现一个方法,找出数组中的第k大和第m大的数字相加之和
Intensive reading of the paper -yolov1:you only look once:unified, real time object detection
Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
Use VRRP technology to realize gateway equipment redundancy, with detailed configuration experiments
[Yuri crack man] brings you easy understanding - deep copy and shallow copy
离线数据仓库从0到1-阶段二软件安装
redis集群的三种方式
els 修改光标、修改图标
There are a group of students in the class who have got the test results in Chinese and mathematics. Please select the students whose total score is the first
Easyexcel sets row hiding to solve the problem of sethidden (true) invalidation
Get twice the result with half the effort: learn the web performance test case design model









