Skip to content

Database Query

WeeSpeed supports the following database query methods:

1. Source Explanation

WeeSpeed directly calls the database methods of Medoo. The following is a brief description. For detailed information, please refer to the official documentation.

2. Query Data

select

Query data.

php
$data = Db::select('table', [
    'column1',
    'column2'
], [
    'column3' => 'value'
]);

get

Query a single piece of data.

php
$data = Db::get('table', [
    'column1',
    'column2'
], [
    'column3' => 'value'
]);

has

Check if data exists.

php
$exists = Db::has('table', [
    'column' => 'value'
]);

count

Count the number of data items.

php
$count = Db::count('table', [
    'column' => 'value'
]);

max

Get the maximum value of a certain column.

php
$max = Db::max('table', 'column', [
    'column2' => 'value'
]);

min

Get the minimum value of a certain column.

php
$min = Db::min('table', 'column', [
    'column2' => 'value'
]);

avg

Get the average value of a certain column.

php
$avg = Db::avg('table', 'column', [
    'column2' => 'value'
]);

sum

Get the sum of a certain column.

php
$sum = Db::sum('table', 'column', [
    'column2' => 'value'
]);

3. Insert Data

insert

Insert a single piece of data.

php
Db::insert('table', [
    'column1' => 'value1',
    'column2' => 'value2'
]);

insertMulti

Insert multiple pieces of data.

php
Db::insertMulti('table', [
    [
        'column1' => 'value1',
        'column2' => 'value2'
    ],
    [
        'column1' => 'value3',
        'column2' => 'value4'
    ]
]);

4. Update Data

update

Update data.

php
Db::update('table', [
    'column1' => 'new_value'
], [
    'column2' => 'value'
]);

5. Delete Data

delete

Delete data.

php
Db::delete('table', [
    'column' => 'value'
]);

6. Aggregate Query

query

Execute a custom SQL query.

php
$data = Db::query('SELECT * FROM table WHERE column = :value', [
    ':value' => 'value'
])->fetchAll();

exec

Execute a custom SQL statement (no return value).

php
Db::exec('UPDATE table SET column = :value WHERE column2 = :value2', [
    ':value' => 'new_value',
    ':value2' => 'value2'
]);

7. Transaction Operations

action

Execute a transaction operation.

php
Db::action(function($database) {
    Db::insert('table1', [
        'column1' => 'value1'
    ]);

    Db::update('table2', [
        'column2' => 'value2'
    ], [
        'column3' => 'value3'
    ]);
});

8. Other Methods

id

Get the ID of the last inserted record.

php
$lastInsertId = Db::id();

info

Get database information.

php
$info = Db::info();

debug

Debug SQL statements.

php
Db::debug()->select('table', '*');

log

Get the query log.

php
$logs = Db::log();

last

Get the last executed SQL statement.

php
$lastQuery = Db::last();

9. Supported Database Types

Medoo supports the following database types:

  • MySQL
  • MariaDB
  • PostgreSQL
  • SQLite
  • SQL Server
  • Oracle
  • Sybase