日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php sql desc,PHP SQL 查询封装

發布時間:2025/3/12 php 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php sql desc,PHP SQL 查询封装 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/**

* SQL 簡單查詢工具類

*

* $tools = new SQLTools("表名", "數據庫操作對象實例");

*$tools->query("字段默認為*") //(如無后續操作此處返回查詢結果集)

* ->where( '條件', PDO參數化查詢參數 ) //(如無后續操作此處返回查詢結果集)

* ->group( 'id' ) //(如無后續操作此處返回查詢結果集)

* ->order( 'id', 'desc' ) //(如無后續操作此處返回查詢結果集)

* ->limit( 0, 100 ) //(如無后續操作此處返回查詢結果集)

* ->toSQL(); // 返回拼接出來的SQL

*

*

*/

defined( 'SQL_TAG_QUERY' ) OR define( 'SQL_TAG_QUERY', 'query' );

defined( 'SQL_TAG_LIMIG' ) OR define( 'SQL_TAG_LIMIT', 'limit' );

defined( 'SQL_TAG_WHERE' ) OR define( 'SQL_TAG_WHERE', 'where' );

defined( 'SQL_TAG_ORDER' ) OR define( 'SQL_TAG_ORDER', 'order' );

defined( 'SQL_TAG_GROUP' ) OR define( 'SQL_TAG_GROUP', 'group' );

// to xx 自己加吧

defined( 'TO_SQL' ) OR define( 'TO_SQL', 'toSQL' );

class SQLTools{

public $id = null;

public $db = null;

public $tableName = null;

private $__code = null;

private $__query = null;

private $__where = null;

private $__param = null;

private $__limit = null;

private $__order = null;

private $__group = null;

/**

* 實例化

* @param $tableName stirng 表名

* @param $db 一個數據庫操作對象,且必須有個叫query的方法,接受兩個參數 sql 及 params

* @param $id 主鍵字段名

*/

public function __construct( $tableName, $db, $id=null ){

$this->db = $db;

$this->id = $id;

$this->tableName = $tableName;

}

public function query( $fields='*', $tableName=null ){

$tableName === null && ( $tableName = $this->tableName );

$this->__query = "SELECT $fields FROM $tableName ";

return $this->__setResultByCallCode( SQL_TAG_QUERY );

}

public function where( $where, $params ){

$this->__where = "WHERE $where ";

$this->__param = $params;

return $this->__setResultByCallCode( SQL_TAG_WHERE );

}

public function order( $fields, $sort ){

$this->__order = "ORDER BY $fields $sort ";

return $this->__setResultByCallCode( SQL_TAG_ORDER );

}

public function group( $fields ){

$this->__group = "GROUP BY $fields";

return $this->__setResultByCallCode( SQL_TAG_GROUP );

}

public function limit( $m, $n ){

$this->__limit = sprintf( 'LIMIT %d,%s ', $m, $n );

return $this->__setResultByCallCode( SQL_TAG_LIMIT );

}

public function toSQL(){ return $this->__setResultByCallCode( TO_SQL ); }

public function clear(){

$this->__code = null;

$this->__query = null;

$this->__where = null;

$this->__param = null;

$this->__limit = null;

$this->__order = null;

$this->__group = null;

}

// 真正查詢的地方

private function __query( $tag ){

$__sql = $this->__query;

$this->__where !== null && ( $__sql .= $this->__where );

$this->__group !== null && ( $__sql .= $this->__group );

$this->__order !== null && ( $__sql .= $this->__order );

$this->__limit !== null && ( $__sql .= $this->__limit );

$result = $tag === TO_SQL ? $__sql : $this->db->query( $__sql, $this->__param );

$this->clear();

return $result;

}

/**

* 通過堆棧信息獲取調用腳本后面調用方法,

* 根據方法生成相關返回對象

* @param $tag sql標簽

* @return object

**/

private function __setResultByCallCode( $tag ){

if( $this->__code !== null ){

return $this->__createResult( $this->__code, $tag );

}

$info = debug_backtrace();

if( !is_array($info) ){

return null;

}

// 找到調用文件索引 ( 這里是通過文件名匹配的,如果改了文件名請自行修改這段代碼 )

$index = -1;

foreach( $info as $counter => $item ){

if( isset($item['file']) ){

if( stripos($item['file'], 'SQLTools.class.php') > 0 ){

$index = $counter + 1; // 下一個item即調用文件

break;

}

}

}

// 沒有找到調用信息

if( $index === -1 ){

return null;

}

// 堆棧中沒有找到相關信息

$caller = $info[$index];

if( !isset($caller['file']) || !file_exists($caller['file']) || !isset($caller['line']) ){

return null;

}

$line = $caller['line'];

$file = @fopen( $caller['file'], "r" );

$counter = 1;

$code = '';

while( ($buffer = fgets($file)) !== false ){

if( $counter >= $line ){

$code .= $buffer;

if( substr( $buffer, -2, 1 ) == ';' ){

goto end;

}

}

$counter++;

}

end: isset( $file ) && @fclose( $file );

$code = str_replace( ' ', '', $code );

$code = str_replace( "\t", '', $code );

$code = str_replace( "\n", '', $code );

$code = explode( '->', $code );

return $this->__createResult( $code, $tag );

}

// 返回$this起到鏈接作用,又判斷當前調用tag是否已經結束

private function __createResult( $code, $tag ){

$this->__code = $code;

foreach( $this->__code as $code){

if( stripos($code, $tag) === 0 && substr( $code, -1 ) === ';' ){ // 判斷查詢結束

return $this->__query( $tag );

}

}

return $this;

}

}

總結

以上是生活随笔為你收集整理的php sql desc,PHP SQL 查询封装的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。