一个不错的MySQL类
包含CRUD,事务,树递归查询,分页等. 演示简单的魅力. 所有的参数都escape了,不存在注入问题.
/*
* Anthony.chen
* 2010 reserved
*/
class DB {
// Query types
const SELECT = 1;
const INSERT = 2;
const UPDATE = 3;
const DELETE = 4;
const T = 't';
const F = 'f';
/*
* Supporting return multiple data;
*/
public static function insert_table($table_name,$data,$return_id='id',$db='default'){
if (!is_array($data)){
return false;
}
if (is_null($return_id)){
self::query(self::SELECT,self::insert($table_name,array_keys($data))->values(array_values($data)))->execute($db);
return true;
}
if (is_string($return_id)){
$id = self::query(self::SELECT,self::insert($table_name,array_keys($data))->values(array_values($data))." returning ".$return_id)->execute($db)->get($return_id);
return $id;
}else{
if (is_array($return_id)){
$ids = implode(',',$return_id);
$r_ids = self::query(self::SELECT,self::insert($table_name,array_keys($data))->values(array_values($data))." returning ".$ids)->execute($db)->current();
return $r_ids;
}
}
return false;
}
public static function update_table($table_name,$id,$data,$id_name='id',$db='default'){
if (!is_array($data)){
return false;
}
return self::update($table_name)->set($data)->where($id_name ,'=',$id)->execute($db);
}
public static function quote($s,$db='default'){
if(!is_array($s)){
return Database::instance($db)->quote($s);
}else{ //Quote array and implode
$_qs = array();
foreach ($s as $ele){
$_qs[] = self::quote($ele,$db);
}
$_quoteString = implode(',',$_qs);
return $_quoteString;
}
}
public static function escape($s,$db='default'){
return Database::instance($db)->escape($s);
}
public static function quote_table($s,$db='default'){
return Database::instance($db)->quote_table($s);
}
public static function getConnection($db = 'default'){
return Database::instance($db)->getConnection();
}
public static function getChildren($table,$returnSql = false ,$pid= '0',$idname='id',$pidname='pid' ,$db='default'){
$_sql = 'select * from '.$table.' where '.$pidname.'='.self::escape($pid,$db).
" and $idname ".DB::escape($pid,$db);
if($returnSql){
return $_sql;
}
$_res = self::query(self::SELECT,$_sql)->execute($db)->as_array();
if($_res){
return $_res;
}else{
return false;
}
}
/*
*
*/
public static function getTree($tableName,$returnSql=false,$startWith='0',$idCol='id',$pidCol='pid', $orderCol='id', $maxDepth=0,$level = 0,$delimiter = ';',$db='default'){
$_funcParas = array();
$_funcParas[] = self::quote($tableName,$db); //TableView
$_funcParas[] = self::quote($idCol,$db); //ID column
$_funcParas[] = self::quote($pidCol,$db); //Parent ID Column
$_funcParas[] = self::quote($orderCol,$db); //Default Order by ASC
$_funcParas[] = self::quote($startWith,$db); //Begin NODE
$_funcParas[] = self::quote($maxDepth,$db); //Begin Depth of traverse
$_funcParas[] = self::quote($delimiter,$db); //Delimitor,default ';'
$_sql = 'select * from connectby('
.implode(',',$_funcParas).')'
.' as t(id int, pid int, level int, branch text, pos int)';
if($level > 0){
$_sql .= ' where level >='.self::quote($level);
}
if($returnSql) return $_sql;
$_res = self::query(self::SELECT,$_sql,true)->execute($db)->as_array();
if($_res){
return $_res;
}else{
return false;
}
}
public static function begin($db='default'){
DB::query(Database::UPDATE, "BEGIN")->execute($db);
}
public static function commit($db='default'){
DB::query(Database::UPDATE, "COMMIT")->execute($db);
}
public static function rollback($db='default'){
DB::query(Database::UPDATE, "ROLLBACK")->execute($db);
}
/**
* Create a new [Database_Query] of the given type.
*
* // Create a new SELECT query
* $query = DB::query(Database::SELECT, 'SELECT * FROM users');
*
* // Create a new DELETE query
* $query = DB::query(Database::DELETE, 'DELETE FROM users WHERE id = 5');
*
* Specifying the type changes the returned result. When using
* `Database::SELECT`, a [Database_Query_Result] will be returned.
* `Database::INSERT` queries will return the insert id and number of rows.
* For all other queries, the number of affected rows is returned.
*
* @param integer type: Database::SELECT, Database::UPDATE, etc
* @param string SQL statement
* @return Database_Query
*/
public static function query($type, $sql,$as_object = false)
{
return new Database_Query($type, $sql,$as_object);
}
/**
* Create a new [Database_Query_Builder_Select]. Each argument will be
* treated as a column. To generate a `foo AS bar` alias, use an array.
*
* // SELECT id, username
* $query = DB::select('id', 'username');
*
* // SELECT id AS user_id
* $query = DB::select(array('id', 'user_id'));
*
* @param mixed column name or array($column, $alias) or object
* @param ...
* @return Database_Query_Builder_Select
*/
public static function select($columns = NULL)
{
return new Database_Query_Builder_Select(func_get_args());
}
/**
* Create a new [Database_Query_Builder_Select] from an array of columns.
*
* // SELECT id, username
* $query = DB::select_array(array('id', 'username'));
*
* @param array columns to select
* @return Database_Query_Builder_Select
*/
public static function select_array(array $columns = NULL)
{
return new Database_Query_Builder_Select($columns);
}
/**
* Create a new [Database_Query_Builder_Insert].
*
* // INSERT INTO users (id, username)
* $query = DB::insert('users', array('id', 'username'));
*
* @param string table to insert into
* @param array list of column names or array($column, $alias) or object
* @return Database_Query_Builder_Insert
*/
public static function insert($table = NULL, array $columns = NULL)
{
return new Database_Query_Builder_Insert($table, $columns);
}
/**
* Create a new [Database_Query_Builder_Update].
*
* // UPDATE users
* $query = DB::update('users');
*
* @param string table to update
* @return Database_Query_Builder_Update
*/
public static function update($table = NULL)
{
return new Database_Query_Builder_Update($table);
}
/**
* Create a new [Database_Query_Builder_Delete].
*
* // DELETE FROM users
* $query = DB::delete('users');
*
* @param string table to delete from
* @return Database_Query_Builder_Delete
*/
public static function delete($table = NULL)
{
return new Database_Query_Builder_Delete($table);
}
/**
* Create a new [Database_Expression] which is not escaped. An expression
* is the only way to use SQL functions within query builders.
*
* $expression = DB::expr('COUNT(users.id)');
*
* @param string expression
* @return Database_Expression
*/
public static function expr($string){
return new Database_Expression($string);
}
/*
* Gettting paginated page
*/
public static function getPage($_sql,&$page,$orderBy ='updated desc', $dataPro='data',$pagePro = 'pagination',
$config = NULL,$db = 'default',$as_object= true){
$_csql = 'select count(1) as c from ('.$_sql.') st';
$_c = DB::query(DB::SELECT,$_csql)->execute($db)->get('c');
if($config){
$config['total_items'] = $_c;
$_pagination = new Pagination($config);
}else{
$config = array();
$config['total_items'] = $_c;
$_pagination = new Pagination($config);
}
$_sql .= ' order by '.$orderBy;
if($_pagination->offset){
$_sql .= ' offset '.$_pagination->offset;
}
$_sql .= ' limit '.$_pagination->items_per_page;
$_data = DB::query(DB::SELECT,$_sql,$as_object)->execute($db)->as_array();
if(!$_data){
$page->{$dataPro} = false;
$page->{$pagePro} = false;
return false;
}
$page->{$dataPro} = $_data;
$page->{$pagePro} = $_pagination;
return true;
}
/*
* Get All roles
* level to control the statrt
*/
public static function getRoleTreeSql($role_id,$quote = false,$role_table,$level=0,$db='default'){
$_sql = 'select id from ('.self::getTree($role_table,true,$role_id,'id','pid','id',
0, //Maxdepth
$level, //Level
';',$db).') utree';
if(!$quote) return $_sql;
else return '('.$_sql.')';
}
/*
* Return sub query on Objects authorization
* Child role objects and owned objects
* Parent control
*/
public static function getCURTreeSql($role_id,$user_id,$role_table,$quote = true,
$role='role_id',$owner = 'owner_id' ,$db='default'){
$_sql = ' '.$role.' in '.self::getRoleTreeSql($role_id,true,$role_table,
1, //Level start with 1
$db). ' or '.$owner.'='.DB::quote($user_id);
if(!$quote) return $_sql;
else return '('.$_sql.')';
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The solutions to discuz database error are: 1. Check the database configuration; 2. Make sure the database server is running; 3. Check the database table status; 4. Back up the data; 5. Clear the cache; 6. Reinstall Discuz; 7. Check the server resources ; 8. Contact Discuz official support. Solving Discuz database errors requires starting from multiple aspects, gradually identifying the cause of the problem, and taking corresponding measures to repair it.

IntelArrowLakeisexpectedtobebasedonthesameprocessorarchitectureasLunarLake,meaningthatIntel'sbrandnewLionCoveperformancecoreswillbecombinedwiththeeconomicalSkymontefficiencycores.WhileLunarLakeisonlyavailableasava

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

Asynchronous processing method of SelectChannelsGo concurrent programming using golang Introduction: Concurrent programming is an important area in modern software development, which can effectively improve the performance and responsiveness of applications. In the Go language, concurrent programming can be implemented simply and efficiently using Channels and Select statements. This article will introduce how to use golang for asynchronous processing methods of SelectChannelsGo concurrent programming, and provide specific

How to hide the select element in jquery: 1. hide() method, introduce the jQuery library into the HTML page, you can use different selectors to hide the select element, the ID selector replaces the selectId with the ID of the select element you actually use; 2. css() method, use the ID selector to select the select element that needs to be hidden, use the css() method to set the display attribute to none, and replace selectId with the ID of the select element.

In this article, we will show you how to reorder multiple columns in PowerQuery by dragging and dropping. Often, when importing data from various sources, columns may not be in the desired order. Reordering columns not only allows you to arrange them in a logical order that suits your analysis or reporting needs, it also improves the readability of your data and speeds up tasks such as filtering, sorting, and performing calculations. How to rearrange multiple columns in Excel? There are many ways to rearrange columns in Excel. You can simply select the column header and drag it to the desired location. However, this approach can become cumbersome when dealing with large tables with many columns. To rearrange columns more efficiently, you can use the enhanced query editor. Enhancing the query

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:
