## 數據庫操作類
~~~
<?php
/**
* Created by gather
* Email: chenruiqiang@yd-x.com
* Phone: 16601180687
* Copyright:源動互通(北京)科技有限公司
* Create Time: 2018/6/6 13:55
*/
namespace test;
class DB{
private static $dbcon = false;
private $host;
private $port;
private $user;
private $pass;
private $db;
private $charset;
private $link;
public function __construct(){
$this->host = "127.0.0.1";
$this->port = "3306";
$this->user = "root";
$this->pass = "xyq07041103";
$this->db = "email";
$this->charset = "utf8";
$this->db_connect();
$this->db_usedb();
$this->db_charset();
}
//連接數據庫
private function db_connect(){
//$host = '', $user = '', $password = '', $database = '', $port = '', $socket = ''
$this->link = mysqli_connect($this->host,$this->user,$this->pass,$this->db,$this->port);
if (!$this->link){
echo "數據庫連接失敗<br>";
echo "錯誤編碼".mysqli_errno($this->link)."<br>";
echo "錯誤信息".mysqli_error($this->link)."<br>";
exit();
}
}
//設置字符集
public function db_charset(){
mysqli_query($this->link,"set names {$this->charset}");
}
//選擇數據庫
public function db_usedb(){
mysqli_query($this->link,"use {$this->db}");
}
//私有的克隆
private function __clone(){
// TODO: Implement __clone() method.
die("clone is not allowed");
}
//公用靜態方法
public static function getInstance(){
if (self::$dbcon == false){
self::$dbcon = new self;
}
return self::$dbcon;
}
//執行sql語句的方法
public function query($sql){
$res = mysqli_query($this->link,$sql);
if(!$res){
echo "sql語句執行失敗<br>";
echo "錯誤編碼是".mysqli_errno($this->link)."<br>";
echo "錯誤信息是".mysqli_error($this->link)."<br>";
}
return $res;
}
//獲得最后一條記錄id
public function getInsertId(){
return mysqli_insert_id($this->link);
}
/**
* 查詢莫個字段
* @param $sql
* @return string or int
*/
public function getOne($sql){
$query = $this->query($sql);
return mysqli_free_result($query);
}
//獲取一行記錄,return array 一維數組
public function getRow($sql,$type="assoc"){
$query = $this->query($sql);
if(!in_array($type,array("assoc",'array','row'))){
die("mysql_query error");
}
$funcname = "mysqli_fetch_".$type;
return $funcname($query);
}
//獲取一條記錄,前置條件通過資源獲取一條記錄
public function getFormSource($query,$type="assoc"){
if(!in_array($type,array("assoc","array","row"))){
die("mysqli_query error");
}
$funcname = "mysqli_fetch_".$type;
return $funcname($query);
}
//獲取多條數據,二維數組
public function getAll($sql){
$query = $this->query($sql);
$list = array();
while ($r=$this->getFormSource($query)){
$list[] = $r;
}
return $list;
}
//選擇所有
public function selectAll($table,$where,$fields='*',$order='',$skip=0,$limit=1000){
if (is_array($where)){
foreach ($where as $key => $val){
if(is_numeric($val)){
$condition = $key.'='.$val;
}else{
$condition = $key.'=\''.$val.'\'';
}
}
}else{
$condition = $where;
}
if (!empty($order)){
$order = " order by ".$order;
}
$sql = "select $fields from $table where $condition $order limit $skip,$limit";
$query = $this->query($sql);
$list = array();
while ($r=$this->getFormSource($query)){
$list[] = $r;
}
return $list;
}
public function insert($table,$data){
$key_str = "";
$v_str="";
foreach ($data as $key=>$v) {
$key_str .= $key.',';
$v_str .= "'$v'".',';
}
$key_str = trim($key_str,',');
$v_str = trim($v_str,',');
$sql = "insert into $table($key_str) values($v_str)";
$this->query($sql);
return $this->getInsertId();
}
public function deleteOne($table,$where){
if(is_array($where)){
foreach ($where as $key=>$val){
$condition = $key.'='.$val;
}
}else{
$condition = $where;
}
$sql = "delete from $table where $condition";
$this->query($sql);
return mysqli_affected_rows($this->link);
}
public function deleteAll($table,$where){
if(is_array($where)){
foreach ($where as $key=>$val){
if(is_array($val)) {
$condition = $key.' in('.implode(',',$val).')';
}else{
$condition = $key.'='.$val;
}
}
}else{
$condition = $where;
}
$sql = "delete from $table where $condition";
$this->query($sql);
return mysqli_affected_rows($this->link);
}
public function update($table,$data,$where,$limit=0){
$str = '';
foreach ($data as $key=>$v){
$str .= "$key='$v'".',';
}
$str = rtrim($str,',');
if (is_array($where)){
foreach ($where as $key=>$val){
if(is_array($val)) {
$condition = $key.' in('.implode(',',$val).')';
}else{
$condition = $key.'='.$val;
}
}
}else{
$condition = $where;
}
if (empty($limit)){
$limit = " limit ".$limit;
}else{
$limit = '';
}
$sql = "update $table set $str where $condition $limit";
// var_dump($sql);
$this->query($sql);
return mysqli_affected_rows($this->link);
}
}
~~~
## 數據表
~~~
CREATE TABLE `order_queue` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`mobile` varchar(2) NOT NULL COMMENT '用戶手機號',
`created_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`status` tinyint(4) NOT NULL COMMENT '0未處理1已處理2處理中',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4
~~~
## 增加訂單order.php文件
~~~
<?php
/**
* Created by gather
* Email: chenruiqiang@yd-x.com
* Phone: 16601180687
* Copyright:源動互通(北京)科技有限公司
* Create Time: 2018/6/6 18:55
*/
//這個文件主要是配送系統處理隊列中的訂單并進行標記的一個文件
require "DB.php";
use test\DB;
$db=DB::getInstance();
//1)先把要處理的記錄更新為等待處理,
$waiting = array('status'=>0);
$data = array(
'status'=>2,
);
$res_lock = $db->update('order_queue',$data,$waiting,2);
//2)我們要選擇出剛剛咱們更新的這些數據,然后進行配送系統的處理。
if ($res_lock){
//選擇出要處理的訂單內容,有配貨系統處理
$res = $db->selectAll('order_queue',$data);
//有配貨系統進行退貨處理
//把訂單更新已完成
$success = array(
'status'=>1,
'updated_at'=>date("Y-m-d H:i:s")
);
$res_last = $db->update('order_queue',$success,$data,2);
if ($res_last){
echo "Success:".$res_last;
}else{
echo "Fail:".$res_last;
}
}else{
echo "沒有需要處理的數據";
}
//3)處理過的更新為已完成。
~~~
## 處理訂單goods.php文件
~~~
<?php
/**
* Created by gather
* Email: chenruiqiang@yd-x.com
* Phone: 16601180687
* Copyright:源動互通(北京)科技有限公司
* Create Time: 2018/6/6 18:55
*/
//這個文件主要是配送系統處理隊列中的訂單并進行標記的一個文件
require "DB.php";
use test\DB;
$db=DB::getInstance();
//1)先把要處理的記錄更新為等待處理,
$waiting = array('status'=>0);
$data = array(
'status'=>2,
);
$res_lock = $db->update('order_queue',$data,$waiting,2);
//2)我們要選擇出剛剛咱們更新的這些數據,然后進行配送系統的處理。
if ($res_lock){
//選擇出要處理的訂單內容,有配貨系統處理
$res = $db->selectAll('order_queue',$data);
//有配貨系統進行退貨處理
//把訂單更新已完成
$success = array(
'status'=>1,
'updated_at'=>date("Y-m-d H:i:s")
);
$res_last = $db->update('order_queue',$success,$data,2);
if ($res_last){
echo "Success:".$res_last;
}else{
echo "Fail:".$res_last;
}
}else{
echo "沒有需要處理的數據";
}
//3)處理過的更新為已完成。
~~~
## shell 文件 good.sh
~~~
#!/bin/bash
date "+%G-%m-%d %H:%M:%S"
cd /Applications/XAMPP/xamppfiles/htdocs/study/order_queue/
php goods.php
~~~
## 定時任務crontab 命令
crontab -e
`*/1 * * * * /Applications/XAMPP/xamppfiles/htdocs/study/order_queue/good.sh >> /Applications/XAMPP/xamppfiles/htdocs/study/order_queue/log.log 2>&1`
- 簡介
- Cookie
- HTML5 LocalStorage
- session
- 當瀏覽器關閉后,Session就銷毀了嗎?
- mysql數據庫保存session
- HTTP協議的由來
- fsockopen異步請求
- http防盜鏈
- Apache偽靜態知識補充
- 大并發量解決方案
- 大型網站是怎樣解決多用戶高并發訪問
- 網站高并發 大流量訪問的處理及解決方法
- 并發數與在線客戶數?注冊用戶數的關系
- 即時聊天程序
- 反向Ajax實現
- ob緩存作用
- 淺聊并發之戰
- php擴展安裝
- php安裝redis擴展
- SQLMap自動化實施SQL注入共計
- 命名空間namespace
- 集群和分布式之【session共享】
- php Redis存儲Session 【1】
- php Redis存儲Session 【2】
- php mysql存儲session【1】
- php緩存
- 文件緩存
- memcache和redis的比較
- 原生session與session in redis對比
- XSS攻擊【1】
- XSS攻擊【2】
- PHP消息隊列
- php+mysql 模擬發送郵件隊列
- php+mysql 模擬訂單處理隊列
- php+redis 模擬秒殺隊列
- RabbitMQ 消息隊列系統
- beanstalkd
- PHP構建即時通訊
- WebSocket協議
- workerman
- PHP變量的作用域
- PHP傳值和傳引用的區別
- PHP匿名函數
- PHP遞歸函數&應用
- PHP單例模式
- PHP性能優化
- RESTful
- 集群
- 增加pgsql擴展
- php.ini路徑查找
- Swoole Compiler
- mysql 主從
- 主從
- mysql-proxy
- window docker環境