Database
Database access is one of the most crucial things you probably need for your Module. This article should explain the most basic ways to interact with the Database.
Reading
fetch_single_row
$user = Panel::getDatabase()->fetch_single_row('users', 'id', 1, \PDO::FETCH_ASSOC);
Parameters:
- table: where to fetch the data from.
- col: what column to filter for.
- val: the value to filter.
- method: what method to use, either return as array, object or both.
You can also pass an array for col and val to do an AND search.
fetch_all
$users = Panel::getDatabase()->fetch_all('users');
Parameters:
- table: fetch all entries from a table
fetch_multi_row
$users = Panel::getDatabase()->fetch_multi_row('users', [
'id',
'name',
], [
'email' => 'me@bennetgallein.de'
])->fetchAll(\PDO::FETCH_ASSOC);
Parameters:
- table: the table to load from
- col: the columns to fetch
- where: array of wheres
custom_query
In case you need raw database access (most likely if you have some more complex queries) you can use the custom_query method.
$users = Panel::getDatabase()->custom_query(
'SELECT * FROM users WHERE email = ?',
['email' => 'me@bennetgallein.de']
)->fetchAll(\PDO::FETCH_ASSOC);
Parameters:
- query: the raw sql query, use
?
to indicate a binding - data: the bindings
- types: optional
Writing
insert
to insert data, specify the table and the data you want to insert
$success = Panel::getDatabase()->insert('users', [
'email' => 'me@bennetgallein.de'
]);
update
$update = Panel::getDatabase()->update('users', ['email' => 'me@bennetgallein.de'], 'id', 2);
Parameters:
- table: the table to update
- data: assoc array of data
- id: the where-column
- val: the value for the where
delete
$del = Panel::getDatabase()->delete('users', 'id', 2);
Parameters:
- table: the table to delete from
- where: the where-column
- id: the value for the where
Utility functions
countRows
count the rows in a table
$users = Panel::getDatabase()->countRows('users');
check_exist
check if a value exists in the database
$userExists = Panel::getDatabase()->check_exist('users', ['email' => 'me@bennetgallein.de']);
Parameters:
- table: the table to check in
- data: assoc array of conditions that need to match
get_last_id
returns the last inserted ID.
$id = Panel::getDatabase()->get_last_id();
Do you require help?
Wether you have encountered a Bug, ran into a problem setting something up or require generall assistance using some of the features, we want to help you with that.
On our Discord-Server you can ask for help of any kind, suggest new ideas for our products or just hangout and chat!