首页 > 数据库 >php简单MYSQL操作类

php简单MYSQL操作类

时间:2023-08-08 11:13:50浏览次数:45  
标签:function Exception return 简单 MYSQL sql php public conn

<?php
/*
简单MYSQL操作类

include './mysqlDb.php';

$mysql = null;
try {
$mysql = new db('localhost', 'root', 'root', 'test');
//SELECT
$mysql->select('SELECT * FROM abc');
//UPDATE
$mysql->update('UPDATE `abc` SET `e` = 101 WHERE `id` =1');
//INSERT
$mysql->insert('INSERT INTO `abc` (`a`, `b`, `c`) VALUES (11, 22, 33)');
//DELETE
$mysql->delete('DELETE FROM `abc` WHERE `id` = 5');
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
*/

class db
{
private $_conn = null;

public function __construct($ip, $user, $password, $db, $port = 3306)
{
try {
$this->_conn = mysqlDb::instance()
->setIp($ip)
->setUser($user)
->setPassword($password)
->setDb($db)
->setPort($port)
->connection();
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}

public function select($sql)
{
try {
return $this->_conn->select($sql);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}

public function update($sql)
{
try {
return $this->_conn->update($sql);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}

public function insert($sql)
{
try {
return $this->_conn->insert($sql);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}

public function delete($sql)
{
try {
return $this->_conn->delete($sql);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
}

class mysqlDb
{
private static $_instance = NULL;
private $_conn = '';
private $_ip = '';
private $_user = '';
private $_password = '';
private $_db = '';
private $_port = 3306;

public function setIp($ip)
{
$this->_ip = $ip;
return $this;
}

public function setUser($user)
{
$this->_user = $user;
return $this;
}

public function setPassword($password)
{
$this->_password = $password;
return $this;
}

public function setDb($db)
{
$this->_db = $db;
return $this;
}

public function setPort($port = 3306)
{
$this->_port = $port;
return $this;
}

public static function instance()
{
if (self::$_instance === NULL) {
self::$_instance = new self();
}
return self::$_instance;
}

public function connection()
{
$this->_conn = new mysqli($this->_ip, $this->_user, $this->_password, $this->_db, $this->_port);

/* check connection */
if ($this->_conn->connect_errno) {
$message = sprintf("Connect failed: %s", $this->_conn->connect_error);
throw new Exception($message);
}

/* change character set to utf8 */
if (!($this->_conn->set_charset("utf8"))) {
$message = sprintf("Error loading character set utf8: %s", $this->_conn->error);
throw new Exception($message);
}
return $this;
}

/**
* 查询
* @param string $sql SQL语句
* @return array 返回二维数组
*/
public function select($sql)
{
$resultArr = array();
$sqlResult = $this->_query($sql);
if ($sqlResult) {
while ($row = $sqlResult->fetch_assoc()) {
$resultArr[] = $row;
}
}
return $resultArr;
}

/**
* 修改
* @param string $sql SQL语句
* @return int 返回影响行数
*/
public function update($sql)
{
$this->_query($sql);
return $this->_conn->affected_rows;
}

/**
* 添加
* @param string $sql SQL语句
* @return int 返回自增ID
*/
public function insert($sql)
{
$this->_query($sql);
return $this->_conn->insert_id;
}

/**
* 删除
* @param string $sql SQL语句
* @return int 返回影响行数
*/
public function delete($sql)
{
$this->_query($sql);
return $this->_conn->affected_rows;
}

/**
* 执行
* @param string $sql SQL语句
* @return 返回执行结果
*/
private function _query($sql)
{
$result = $this->_conn->query($sql);
if (!$result) {
$message = 'Mysql Error Code:' . $this->_conn->errno . ', Error Message:' . $this->_conn->error;
throw new Exception($message);
}
return $result;
}
}

标签:function,Exception,return,简单,MYSQL,sql,php,public,conn
From: https://www.cnblogs.com/WebLinuxStudy/p/17613625.html

相关文章

  • centos安装php php-fpm 以及 配置nginx
    下载php源码包http://www.php.net/downloads.php安装phptar-xvfphp-5.5.13.tar.bz2cdphp-5.5.13./configure--prefix=/usr/local/php--with-config-file-path=/etc--enable-inline-optimization--disable-debug--disable-rpath--enable-shared--enable-opcache--......
  • php优化递归算法优化
    2023年8月7日13:59:31因为最近开发自己的一些常用系统,所以为了自由度较高一点,经常分类都是无限层级,所以递归用的比较多,但是发现当分类大于三层,数据1万以上递归就会很慢,所以一直在寻求优化算法,使用使用chagpt优化的算法,基本无法使用,后续想到用php原生函数来使用,结果性能飙升数据......
  • c#操作excel方式一:stream简单读写excel
    需要命名空间usingSystem.IO;界面:记得添加openFileDialog注意名字,改成跟代码里的对应写文件按钮代码:privatevoidbutton2_Click(objectsender,EventArgse){inti=0;StreamWriteraFile=null;aFile=File.CreateTex......
  • 性能测试Mysql之profiling参数
    一、查看profiling状态mysql>select@@profiling;0:表示为关闭1:表示开启二、开启profilingmysql>SETprofiling=1;三、showprofiles命令mysql>showprofiles;+----------+------------+--------------------------------------------+|Query_ID|Duration|......
  • linux 如何创建php文件
    首先,需要先安装PHP。在Linux中,你可以通过使用命令行工具来安装PHP。具体方法如下: sudoapt-getupdatesudoapt-getinstallphp以上命令会自动安装PHP并将其设置为默认选项。一旦你安装好了PHP,你就可以开始创建PHP文件了。在Linux中,你可以使用任何文本编辑器来创建PHP文件......
  • No input file specified. thinkphp 高版本正则重写问题
    Noinputfilespecified.问题描述:使用TP框架做项目时,在启用REWRITE的伪静态功能的时候,首页可以访问,但是访问其它页面的时候,就提示:“Noinputfilespecified.”原因在于使用的PHP5.6是fast_cgi模式,而在某些情况下,不能正确识别path_info所造成的错误默认的.htaccess里面的规则:......
  • MySQL插入1000万条数据,用PHP如何做才能保证性能的最优
    插入大量数据时,确保性能最优是很重要的。下面是几种在PHP中快速向MySQL插入大量数据的优化方案:使用多行插入:最简单的方法是使用多行插入语句,将多条记录一次性插入到数据库。这比逐条插入要快得多,因为减少了连接和查询的开销。$values = [];for ($i = 0; $i < 1000000......
  • 何时使用Elasticsearch而不是MySql
    MySQL和Elasticsearch是两种不同的数据管理系统,它们各有优劣,适用于不同的场景。本文将从以下几个方面对它们进行比较和分析:数据模型查询语言索引和搜索分布式和高可用性能和扩展性使用场景数据模型MySQL是一个关系型数据库管理系统(RDBMS),它使用表(table)来存储结构化的......
  • 冒泡排序(简单概叙)
    #include<stdio.h>voidbubble_sort(inta[],intc){inte=0;for(e=0;e<c-1;e+=1){inth=0;intf=0;for(f=0;f<c-1-e;f+=1){if(a[f]>a[f+1])//如果换成变量e会怎么样?......
  • 软件测试|MySQL WHERE条件查询详解:筛选出需要的数据
    简介在数据库中,我们常常需要从表中筛选出符合特定条件的数据,以便满足业务需求或获取有用的信息。MySQL提供了WHERE条件查询,使我们能够轻松地筛选数据。本文将详细介绍MySQLWHERE条件查询的用法和示例,帮助大家更好地理解和应用这一功能。WHERE条件查询的基本语法SELECT列1,列2,.......