【参考】微信 - 数据库 -官方封装接口说明:
前言:微信对SQL的数据库进行了标准封装PDO:本节把接口源码Copy出来简单分析,作为,以后开发的参考。
<?php
namespace QCloud_WeApp_SDK\Mysql;
use \Exception;
use \PDO;
use QCloud_WeApp_SDK\Conf;
use QCloud_WeApp_SDK\Constants;
/**
* 简单的使用 PDO 操作 MySQL 数据库类
* 类为静态类,全部静态调用
*/
class Mysql
{
private static $conn;
public static function getInstance () {
if (!self::$conn) {
$mysql = Conf::getMysql();
$_host = $mysql['host'];
$_port = $mysql['port'];
$_user = $mysql['user'];
$_pass = $mysql['pass'];
$_char = $mysql['char'];
$_db = $mysql['db'];
$dsn = "mysql:host=$_host;dbname=$_db;port=$_port;charset=$_char";
try {
self::$conn = new PDO($dsn, $_user, $_pass);
} catch (PDOException $e) {
throw new Exception(Constants::E_CONNECT_TO_DB . ': '. $e->getMessage());
}
}
return self::$conn;
}
/**
* 插入数据到数据库
* @param string $tableName 数据库名
* @param array $data 要插入的数据
*/
public static function insert ($tableName, $data) {
if (gettype($tableName) !== 'string' || gettype($data) !== 'array') {
throw new Exception(Constants::E_CALL_FUNCTION_PARAM);
}
$prepareData = self::prepare($data);
$prepareFieldsStr = implode(', ', array_keys($prepareData));
$fieldsStr = implode(', ', array_keys($data));
$sql = "INSERT INTO `$tableName` ($fieldsStr) VALUES ($prepareFieldsStr)";
// 执行 SQL 语句
$query = self::raw($sql, $prepareData);
return $query->rowCount();
}
/**
* 查询多行数据
* @param string $tableName 数据库名
* @param array $columns 查询的列名数组
* @param array|string $condition 查询条件,若为字符串则会被直接拼接进 SQL 语句中,支持键值数组
* @param string $operator condition 连接的操作符:and|or
* @param string $suffix SQL 查询后缀,例如 order, limit 等其他操作
* @return array
*/
public static function select ($tableName, $columns = ['*'], $conditions = '', $operator = 'and', $suffix = '') {
if ( gettype($tableName) !== 'string'
|| (gettype($conditions)!== 'array' && gettype($conditions) !== 'string')
|| gettype($columns) !== 'array'
|| gettype($operator) !== 'string'
|| gettype($suffix) !== 'string') {
throw new Exception(Constants::E_CALL_FUNCTION_PARAM);
}
list($condition, $execValues) = array_values(self::conditionProcess($conditions, $operator));
$column = implode(', ', $columns);
// 拼接 SQL 语句
$sql = "SELECT $column FROM `$tableName`";
// 如果有条件则拼接 WHERE 关键则
if ($condition) {
$sql .= " WHERE $condition";
}
// 拼接后缀
$sql .= " $suffix";
// 执行 SQL 语句
$query = self::raw($sql, $execValues);
$allResult = $query->fetchAll(PDO::FETCH_OBJ);
return $allResult === NULL ? [] : $allResult;
}
/**
* 查询单行数据
* @param string $tableName 数据库名
* @param array $columns 查询的列名数组
* @param array|string $condition 查询条件,若为字符串则会被直接拼接进 SQL 语句中,支持键值数组
* @param string $operator condition 连接的操作符:and|or
* @param string $suffix SQL 查询后缀,例如 order, limit 等其他操作
* @return object
*/
public static function row ($tableName, $columns = ['*'], $conditions = '', $operator = 'and', $suffix = '') {
$rows = self::select($tableName, $columns, $conditions, $operator, $suffix);
return count($rows) === 0 ? NULL : $rows[0];
}
/**
* 更新数据库
* @param string $tableName 数据库名
* @param array $updates 更新的数据对象
* @param array|string $condition 查询条件,若为字符串则会被直接拼接进 SQL 语句中,支持键值数组
* @param string $operator condition 连接的操作符:and|or
* @param string $suffix SQL 查询后缀,例如 order, limit 等其他操作
* @return number 受影响的行数
*/
public static function update ($tableName, $updates, $conditions = '', $operator = 'and', $suffix = '') {
if ( gettype($tableName) !== 'string'
|| gettype($updates) !== 'array'
|| (gettype($conditions)!== 'array' && gettype($conditions) !== 'string')
|| gettype($operator) !== 'string'
|| gettype($suffix) !== 'string') {
throw new Exception(Constants::E_CALL_FUNCTION_PARAM);
}
// 处理要更新的数据
list($processedUpdates, $execUpdateValues) = array_values(self::conditionProcess($updates, ','));
// 处理条件
list($condition, $execValues) = array_values(self::conditionProcess($conditions, $operator));
// 拼接 SQL 语句
$sql = "UPDATE `$tableName` SET $processedUpdates";
// 如果有条件则拼接 WHERE 关键则
if ($condition) {
$sql .= " WHERE $condition";
}
// 拼接后缀
$sql .= " $suffix";
// 执行 SQL 语句
$query = self::raw($sql, array_merge($execUpdateValues, $execValues));
return $query->rowCount();
}
/**
* 删除数据
* @param string $tableName 数据库名
* @param array|string $condition 查询条件,若为字符串则会被直接拼接进 SQL 语句中,支持键值数组
* @param string $operator condition 连接的操作符:and|or
* @param string $suffix SQL 查询后缀,例如 order, limit 等其他操作
* @return number 受影响的行数
*/
public static function delete ($tableName, $conditions, $operator = 'and', $suffix = '') {
if ( gettype($tableName) !== 'string'
|| ($conditions && gettype($conditions)!== 'array' && gettype($conditions) !== 'string')
|| gettype($operator) !== 'string'
|| gettype($suffix) !== 'string') {
throw new Exception(Constants::E_CALL_FUNCTION_PARAM);
}
// 处理条件
list($condition, $execValues) = array_values(self::conditionProcess($conditions, $operator));
// 拼接 SQL 语句
$sql = "DELETE FROM `$tableName` WHERE $condition $suffix";
// 执行 SQL 语句
$query = self::raw($sql, $execValues);
return $query->rowCount();
}
/**
* 执行原生 SQL 语句
* @param string $sql 要执行的 SQL 语句
* @param array $data SQL 语句的参数值
*/
【案】这一题我要单独讨论一下,之前有尝试去用标准的sql语句+PHP的办法,去操控数据库,结果微信会报错,这样的话,我就在想,如果需要好几个suj数据库如何连接呢,这里,我们看到,其实yuan原生的也可以的,只不过需要你用他这个封装的一个适配接口了。
public static function raw ($sql, $execValues = []) {
$query = self::getInstance()->prepare($sql);
$result = $query->execute($execValues);
if ($result) {
return $query;
} else {
$error = $query->errorInfo();
throw new Exception(Constants::E_EXEC_SQL_QUERY . ': ' . $error[2]);
}
}
/**
* 按照指定的规则处理条件数组
* @example ['a' => 1, 'b' => 2] 会被转换为 ['a = :a and b = :b', [':a' => 1, ':b' => 2]]
* @param array|string $conditions 条件数组或字符串
* @param string $operator condition 连接的操作符:and|or
*/
private static function conditionProcess ($conditions, $operator = 'and') {
$condition = '';
$execValues = [];
if (gettype($conditions) === 'array') {
$cdt = [];
foreach ($conditions as $key => $value) {
if (gettype($value) === 'number') {
array_push($cdt, $value);
} else {
array_push($cdt, $key . ' = :' . $key);
$execValues[$key] = $value;
}
}
$condition = implode(' ' . $operator . ' ', $cdt);
} else {
$condition = $conditions;
}
return [
$condition,
self::prepare($execValues)
];
}
/**
* 转换数据为 PDO 支持的 prepare 过的数据
* @example ['a' => 1] 会被转换为 [':a' => 1]
* @param array $dataArray 要转换的数据
*/
private static function prepare ($dataArray) {
$prepareData = [];
foreach ($dataArray as $field => $value) {
$prepareData[':' . $field] = $value;
}
return $prepareData;
}
}
【参考】微信 - 数据库 -官方封装接口说明:相关推荐
- 微信小程序 封装接口
1.util-util.js //封装接口 let baseURL = 'http://127.0.0.1:3000/'; //接口路径 let request = function (url, op ...
- 微信公众平台消息接口开发(4)天气预报
一.请求数据 首先需要能有取得天气数据的接口,这样的接口网上有很多.比如google, yahoo,天气网都提供天气接口 方倍工作室的API已经能提供全国各地的天气预报,使用方倍的API无需再建立城市 ...
- 微信公众平台消息接口开发(2)-封装weixin.class.php
微信公众平台消息接口开发(2)-封装weixin.class.php 一.封装weixin.class.php 由于微信公众平台的通信使用的是特定格式的XML数据,每次接受和回复都要去做一大堆的数据处 ...
- 微信小程序api接口调用用封装
微信小程序api接口调用用封装 1. 在 02-项目 下新建request目录及index.js文件 1.1 02-项目\request\index.js 1.2.index.js // 同时发送异步 ...
- 微信官方支付接口配置教程
微信官方支付接口配置教程 视频地址 https://www.bilibili.com/video/BV1pe411p7EJ/ 微信官方支付配置教程 1,我们需要四个参数 APPID Appsecret ...
- 微信公众号扫一扫封装接口
一.微信准备功能 1.准备备案域名以及80端口服务器 本人准备是花生壳6元测试版 注册花生壳流程本人博客介绍花生壳IP配置流程 2.申请一个公众号 本人申请为个人订阅号(搜索公众号即可注册) 3.公众 ...
- 微信公众平台服务器的官方示例代码,微信公众平台API接口(示例代码)
简介 微信公众平台消息接口为开发者提供了一种新的消息处理方式.微信公众平台消息接口为开发者提供与用户进行消息交互的能力.对于成功接入消息接口的微信公众账号,当用户发消息给公众号,微信公众平台服务器会使 ...
- php网站怎么对接微信群,PHP对接微信公众平台消息接口开发流程教程
PHP(外文名:PHP: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于学习,使 ...
- php对接微信提醒,PHP对接微信公众平台消息接口开发流程教程
PHP对接微信公众平台消息接口开发流程教程 发布于 2015-02-15 08:54:13 | 157 次阅读 | 评论: 1 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: Hyperte ...
最新文章
- 计算机书籍-老年人编程自学书籍
- 网络国际治理系列 | WTO电子商务谈判合并文本数据跨境流动部分
- 1127: 第三章:再见,林静!
- Nacos配置的优先级
- mysql begin end 用法_超实用的Mysql动态更新数据库脚本的示例讲解(推荐)
- XX市公共租赁住房信息管理系统模板
- MongoDB删除文档(非常详细~)
- 每一个梦想都要让它长上翅膀
- python自动化交易_python 优矿自动化交易
- UE4莫名其妙崩溃的解决办法
- 利用cs美化自己的界面
- 中山大学计算机类专业是什么,中山大学2017年计算机类专业自主招生条件及专业优势...
- 微信小程序开发工具win10下编译非常慢解决方法
- excel——设置单元格格式
- 安装软件时,“Windows Installer Coordinator”一直循环
- 虚拟麦克风音频输入_全新职业级 罗技G PRO X游戏耳机麦克风震撼上市
- ERJ | 马来西亚三城室内环境微生物/代谢产物与初中生哮喘的关联
- 绝世唐门漫画的王秋儿
- 计算机毕业设计(附源码)python信用卡逾期数据处理分析系统
- DirectX11_API流程入门篇