Query Builder for ThinkPHP5.0 Database Operation

Query Builder for ThinkPHP5.0 Database Operation

  Think PHP5.0 query builder uses PDO parameter binding to facilitate database operations, so that the application is not immune to SQL injection, so incoming parameters do not require additional transfer special characters

app\index\controller\index\index.php

public function db()
    {
    // Insert record
    
        // $result = Db::table('think_data')->insert(['name'=>'Huang Xiaoming','status'=>1]);
        // dump ($result);


    //Update operation
    
        // $result = Db::table('think_data')->where ('id',1)->update(['name'=>'Andy Lau','status' =>0]);
        // dump($result);


    //Query (if you don't write the query condition, it will come out as a two-dimensional array, if you write the query condition, it will come out as a value)
    
        // $result = Db::table ('think_data')->where('id',1)->select();
        
    //delete operation
        // $result = Db::table('think_data')->where('id',1)- >delete();

    }

Since the database prefix is ​​set to think_ in the database configuration file, the table method can be changed to the name method, so that the CURD code will not be changed due to the modification of the database prefix, for example:

The code can be further simplified if the system helper function db is used

(The db helper function will relink the database every time by default, so multiple calls should be avoided as much as possible)



Chained operations (can complete complex database query operations)

The chained operations are in no particular order, as long as they are called before the query method (select method), so the following query is equivalent.



1. Query data



2. Add data

Add multiple records


(put the variable before the call)


3. Update data

update the value of a field

The setField method returns the number of affected data, and returns 0 if no data field is modified;

$result = Db::name('data')->where('id',1)->setField('name','thinkphp');

Increment or decrement the value of a field

$result = Db::name('data')->where('id',6)->setInc('stadus');
        //The number selected by default plus one
$result = Db::name('data' )->where('id',7)->setDec('stadus',2);  
        //The original is four, in the original base -2

Time: 2018.4.21


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324684345&siteId=291194637