Manipulate with your data using simple methods
Powerful actions in simple Active Record style. No any SQL writing. Fast and low-memory usage, compact and cleaner resulting code.
- Upload sdba folder on your server.
- Open db_config.php and configure connection with your database.
public static $dbname = 'dbname'; // Your database name
public static $dbuser = 'dbuser'; // Your database username
public static $dbpass = 'dbpass'; // // Your database password
public static $dbhost = 'localhost'; // Your database host, 'localhost' is default.
public static $dbencoding = 'utf8'; // Your database encoding, default is 'utf8'. Do not change, if not sure.
- include sdba.php in your script
Create object of your table, for example - table users. Lets retrieve all rows from users
and write in $users_list variable.
$users = DB::table('users'); // creating table object
$users_list = $users->get(); // retrieving all rows from users table
The script allows you to organize the work with database with a simple methods and without writing queries manually.
- Simple insert, update and delete
- Batch inserts and updates
- Where conditions and fulltext search
- Sorting and grouping
- Joins of multiple tables
- Foreign keys updating
- Checking the uniqueness of records
- Concatenation and group concatenation
- Min, max, sum and total
- Increments and decrements
- You do not need to remember about tildes and quotes
System Requirements
- PHP5
- MySQL5
- mysqli extention
Get rows from table as list of associative arrays.
array get ( [int $limit] [, int $start] )
Will be loaded only limit rows, stating from start. If both parameters in not set, will be loaded all found rows.
$articles
= Sdba::table(``'articles'``);
$list
=
$articles``->get(10)
// loads 10 articles
Get one row from table as associative array.
array get ( void )
Method has no parameters
$articles
= Sdba::table(``'articles'``);
$item
=
$articles``->get_one()
// loads 1 article
Get rows from table as list values.
array get ( mixed $field [, int $limit] [, int $start] [, string $table] )
field - selected field, can be single field(strind), or array(key=>value) and will be placed in key=>value list. Will be loaded only limit rows, stating from start. If both parameters in not set, will be loaded all found rows; table - sets a table, that's different from current instance.
$articles
= Sdba::table(``'articles'``);
$list
=
$articles``->get(10)
// loads 10 articles
Get one row from table as associative array.
string get ( string $field [, string $table] )
field - selected field; table - sets a table, that's different from current instance.
$articles
= Sdba::table(``'articles'``);
$item
=
$articles``->get_one()
// loads 1 article
Returns count of rows in a table.
int total ( void )
Method has no parameters
$articles
= Sdba::table(``'articles'``);
$count
=
$articles``->total();
// returns number of rows in table
Deletes rows from table. You can define specific rows using where() method.
void delete( [int $limit] [, int $start] [, string $table] )
limit - maximum rows for deleting, start - start number of row, table - sets a table, that's different from current instance
$articles
= Sdba::table(``'articles'``);
$articles``->where(``'id'``, 55);
$articles``->``delete``();
Inserts row(s) in database.
void insert( [array $arr] [, string $table] )
arr - array of rows values or list of arrays, table - sets a table, that's different from current instance
$users
= Sdba::table(``'users'``);
$data
=
array``(``'name'``=>``'John'``,``'email'``=>``'john@example.com'``);
$users``->insert(``$data``);
// or batch insert
$data
=
array``(
array``(``'name'``=>``'John'``,``'email'``=>``'john@example.com'``);
array``(``'name'``=>``'Bob'``,``'email'``=>``'bob@example.com'``);
array``(``'name'``=>``'Kate'``,``'email'``=>``'kate@example.com'``);
);
$users``->insert(``$data``);
Get insert id of last inserted item
int insert_id( void )
$new_user
=
array``(``'email'``=>``'user@example.com'``,
'password'``=>``'123456'``);
$users
= Sdba::table(``'users'``);
$users``->insert(``$new_user``);
$user_id
=
$users``->insert_id();
Updates row(s) in database. You can define specific rows using where() method.
void update( array $arr [, int $limit] [, int $start] [, string $table] )
arr - array of rows values or list of arrays, limit - maximum rows for updating, start - start number of row, table - sets a table, that's different from current instance
$users
= Sdba::table(``'articles'``);
$users``->where(``'created <'``,
'2012-10-30'``);
$data
=
array``(``'published'``=> 0);
$users``->update(``$data``);
Rebuilds relations for foreign key.
void update_fk(string $left_key_name, string $left_key_val, string $right_key_name, mixed $right_key_val [, string $table] )
left_key_name - name of foreign key, left_key_val - value of foreign key, right_key_name - name of relation key, right_key_val - new relation values (can be comma-separated string or array), table - sets a table, when it is different from current instance
$fk_articles_categories
= Sdba::table(``'articles_fk'``);
$fk_articles_categories``->update_fk(``'art_id'``,15,``'cat_id'``,``'2,3,8,12'``);
//or
$keys
=
array``(``'2'``,``'3'``,``'8'``,``'12'``);
$fk_articles_categories``->update_fk(``'art_id'``,15,``'cat_id'``,``$keys``);
Inserts row(s) in database or updates when row already exist.
void set( array $arr )
arr - list of arrays, duplicate rows will be updated
$users
= Sdba::table(``'users'``);
$data
=
array``(
array``(``'id'``=>5,``'name'``=>``'John'``,``'email'``=>``'john@example.com'``);
//will be updated, if 'id' found
array``(``'name'``=>``'Bob'``,``'email'``=>``'bob@example.com'``);
// will be inserted
array``(``'name'``=>``'Kate'``,``'email'``=>``'kate@example.com'``);
// will be inserted
);
$users``->set(``$data``);
Sets field, that will bee loaded from database, or not will be loaded when reverse is true.
object fields( mixed $fields [, bool $reverse] [, string $table] )
fields - fields from table (can be comma-separated string or array), reverse - reversing method action, table - sets a table, when it is different from current instance
$users
= Sdba::table(``'users'``);
$users``->fields(``'name'``,``'email'``);
// will extract only name and email columns
$user_list
=
$users``->get();
Sets alias for field in result set
object alias(mixed $field, string $alias [, string $table] )
fields - result field, can be string or field=>alias array; alias - new name of field in result set; table - table, when it is different from current instance.
$users
= Sdba::table(``'users'``);
$users``->alias(``'id'``,``'user_id'``);
$users``->get();
Sets where condition. Affects the methods get, get_one, total, update, delete.
object where( mixed $field [, string $value] [, string $table] [, bool $no_quotes] [, string $join_with] [, string $operator] )
field - table field (can be string with operator, or field=>value array), value - condition value, table - table, when it is different from current instance, no_quotes - prevent value from inserting quotes (need for use mysql functions in where, like NOW() etc), join_with - operator for joining condition, operator - condition operator, system feature.
$users
= Sdba::table(``'users'``);
$users``->where(``'active'``, 1);
$user_list
=
$users``->get();
Alias of where(), used for convenience and readability.
object and_where( string $field [, string $value] [, string $table] [, bool $no_quotes] )
$users
= Sdba::table(``'users'``);
$users``->where(``'active'``, 1)->and_where(``'sex !='``,``'man'``);
$user_list
=
$users``->get();
Alias of where(), used for convenience and readability.
object or_where( string $field [, string $value] [, string $table] [, bool $no_quotes] )
$users
= Sdba::table(``'users'``);
$users``->where(``'active'``, 0)->or_where(``'banned'``,1);
$user_list
=
$users``->get();
Creates like condition, based on where().
object like( string $field, string $value [, string $table] [, array $pattern] [, string $join_with] )
field - table field (can be string with operator, or field=>value array), value - condition value, table - table, when it is different from current instance, pattern - mask for like operation(array with two values - first and last symbol, default is array('%','%')), join_with - operator for joining condition.
$users
= Sdba::table(``'users'``);
$users``->like(``'name'``,
'Bob'``));
// will find 'bob','bobby','bob miller','crazy bob', etc
$user_list
=
$users``->get();
Alias of like(), used for convenience and readability.
object not_like( string $field, string $value [, string $table] [, array $pattern] )
Alias of like(), used for convenience and readability.
object and_like( string $field, string $value [, string $table] [, array $pattern] )
Alias of like(), used for convenience and readability.
object and_not_like( string $field, string $value [, string $table] [, array $pattern] )
Alias of like(), used for convenience and readability.
object or_like( string $field, string $value [, string $table] [, array $pattern] )
Alias of like(), used for convenience and readability.
object or_not_like( string $field, string $value [, string $table] [, array $pattern] )
Creates like condition, based on where().
object where_in( string $field, mixed $value [, string $table] )
field - table field (can be string with operator, or field=>value array), value - condition values(can be comma-separated string or array), table - table, when it is different from current instance.
$users
= Sdba::table(``'users'``);
$users``->like(``'name'``,
'Bob'``));
// will find 'bob','bobby','bob miller','crazy bob', etc
$user_list
=
$users``->get();
Alias of where_in**()**, used for convenience and readability.
object where_in( string $field, string $value [, string $table] )
Alias of where_in**()**, used for convenience and readability.
object and_in( string $field, string $value [, string $table] )
Alias of where_in**()**, used for convenience and readability.
object and_not_in( string $field, string $value [, string $table] )
Alias of where_in**()**, used for convenience and readability.
object or_in( string $field, string $value [, string $table] )
Alias of where_in**()**, used for convenience and readability.
object or_not_in( string $field, string $value [, string $table] )
Allow to create subcondition in where() part (opens '(' in sql query)
object open_sub( [string $join_with] )
join_with - (optional) sets 'glue' between subcondition and other ones in where() side of query, default is 'AND'
$users
= Sdba::table(``'users'``);
$users``->where(``'name'``,
'Bob'``));
$users``->open_sub();
$users``->where(``'name'``,
'Bob'``));
$users``->or_where(``'name'``,
'Jack'``));
$users``->close_sub();
$user_list
=
$users``->get();
Closes sub condition.
object close_sub( void )
Alias of open_sub( 'AND' ), used for convenience and readability.
object and_sub( void )
Alias of open_sub( 'OR' ), used for convenience and readability.
object or_sub( void )
Creates fultext search (working with fulltext index only)
object fulltext( mixed $fields, string $phrase [, string $mode] [, string $table] )
fields - comma-separated string or array of fieldnames, phrase - search string, mode - search mode, default is natural (natural language mode), can be boolean (boolean mode) or expansion (natural language mode with query expansion); table - table, when it is different from current instance.
$articles
= Sdba::table(``'articles'``);
$articles``->fulltext(``'title,text'``,
'Hollidays with Evanto'``));
$articles_list
=
$articles``->get();
Sorts your result data
object sort_by( string $field [, string $direction] [, string $table] )
field - sort field; direction - sort direction (asc, desc); table - table, when it is different from current instance.
$articles
= Sdba::table(``'articles'``);
$articles``->sort_by(``'title'``);
$articles``->sort_by(``'created'``,``'desc'``);
$articles_list
=
$articles``->get();
Randomizing of your result data.
object random( void )
$articles
= Sdba::table(``'articles'``);
$articles``->random();
$articles_list
=
$articles``->get(5);
// get 5 random articles
Select multiple tables with join fearure.
object join( string $type, string $field, string $join_table, string $join_field [, string $left_table] [, string $alias ] [, mixed $right_fields] [, bool $reverse_fields] )
type - joining type, can be any like LEFT, RIGHT, INNER etc, but better to use alias methods (left_join(), right_join(), inner_join()); field - join field in main table; join_table - joined table; join_field - relation field in joined table; left_table - main table, when it is different from current instance; alias - use alias, if you want to join same table more once; right_fields - selected fields from joined table (see fields() method); reverse for selected fields for joined table (see fields() method).
See examples for left_join, inner_join
Alias of join**()**, used for convenience and readability.
object left_join( string $field, string $join_table, string $join_field [, string $left_table] [, string $alias ] [, mixed $right_fields] [, bool $reverse_fields] )
$articles
= Sdba::table(``'articles'``);
$articles``->left_join(``'cat_id'``,``'categories'``,``'cid'``);
// selects articles with categories
$articles_list
=
$articles``->get();
Alias of join**()**, used for convenience and readability.
object right_join( string $field, string $join_table, string $join_field [, string $left_table] [, string $alias ] [, mixed $right_fields] [, bool $reverse_fields] )
Alias of join**()**, used for convenience and readability.
object inner_join( string $field, string $join_table, string $join_field [, string $left_table] [, string $alias ] [, mixed $right_fields] [, bool $reverse_fields] )
$users
= Sdba::table(``'users'``);
$users``->left_join(``'id'``,``'profiles'``,``'user_id'``);
// users with profile
$users_list
=
$users``->get();
Groups your result data.
object group_by( string $field [, string $table] )
field - group field; table - table, when it is different from current instance.
$users
= Sdba::table(``'users'``);
$users``->inner_join(``'id'``,``'articles'``,``'user_id'``);
$users``->group_by(``'id'``);
// removes duplicates, when user wrote more one article
$users_list
=
$users``->get();
// users who wrote articles
Selects sum for selected column
mixed sum(string $field [, string $alias] [, string $table] )
field - selected column, alias - alias of sum for get(), get_one result sets; table - table, when it is different from current instance.
$cart
= Sdba::table(``'cart'``);
$total
=
$cart``->sum('price);
mixed max(string $field [, string $alias] [, string $table] )
field - selected column, alias - alias of max for get(), get_one result sets; table - table, when it is different from current instance.
$cart
= Sdba::table(``'cart'``);
$max
=
$cart``->max('price);
mixed min(string $field [, string $alias] [, string $table] )
field - selected column, alias - alias of min for get(), get_one result sets; table - table, when it is different from current instance.
$cart
= Sdba::table(``'cart'``);
$min
=
$cart``->min('price);
Concatenate fields in one
mixed concat( mixed $fields , string $alias [, string $separator] [, string $table] )
fields - concatenated fields, can be comma-separated string or array; alias - name of field in result set; separator - values separator, default is comma; table - table, when it is different from current instance.
$users
= Sdba::table(``'users'``);
$users``->concat(``'first_name,last_name'``,``'full_name'``,``' '``);
$users_list
=
$users``->get();
Concatenate fields from group in one
mixed group_concat( string $field [, string $alias] [, string $separator] [, string $table] [, bool $distinct] [, string $order_by] [, string $direct] )
field - concatenated column; alias - name of field in result set; separator - values separator, default is comma; table - table, when it is different from current instance; distinct - remove duplicates from group; order_by - column for group sorting; direct - sort direction (asc, desc).
$users
= Sdba::table(``'users'``);
$users``->left_join(``'id'``,``'avards_fk'``,``'user_id'``);
// fk
$users``->inner_join(``'avard_id'``,``'avards'``,``'id'``,``'avards_fk'``);
// avards table
$users``->group_concat(``'avard_name'``);
$users``->group_by(``'id'``);
$users_list
=
$users``->get();
Checking value for uniqueness
bool is_unique( string $field, string $value [, string $table] )
field - name of checked field; value - checked value; table - table, when it is different from current instance.
Returns true, when value is uniqueness, or false if not.
$users
= Sdba::table(``'users'``);
$bool
=
$users``->is_unique(``'email'``,``'john@example.com'``);
// return true or false
Increment of row(s) value
void increment( string $field [, int $num] [, string $table] )
field - field for action; $num - increment number (default is 1); table - table, when it is different from current instance.
$articles
= Sdba::table(``'articles'``);
$articles``->where(``'id'``,55);
$articles``->increment(``'views'``);
Decrement of row(s) value
void decrement( string $field [, int $num] [, string $table] )
field - field for action; $num - decrement number (default is 1); table - table, when it is different from current instance.
$users
= Sdba::table(``'users'``);
$users``->where(``'id'``,118);
$users``->decrement(``'raiting'``,10);
Add DISTINCT operator in query
object distinct( void )
Method has no parameters
Change autoreset status
object auto_reset( [bool $state] )
state - true to enable, false to disable
$users
= Sdba::table(``'users'``);
$users``->auto_reset(true);
Resets conditions (fields, wheres, sort_by, group_by, joins)
object reset( void )
$users
= Sdba::table(``'users'``);
...some actions...
$users``->reset();
Executes manual query
object query( string $query )
query - sql query.
$db
= Sdba::db();
$db``->query(```'TRUNCATE TABLE
articles'```);
// clear articles table
Get result set of manual query as list of arrays
array result()
$db
= Sdba::db();
$db``->query(```"SELECT * FROM
users"```);
$users
=
$db``->result();
Get one result row of manual query as associative array
array row()
$db
= DB::db();
$db``->query(```"SELECT * FROM
usersWHERE
id = 34"```);
$user
=
$db``->row();
Escape special chars for safety queries. Adds quotes automatically. Need only for manual queries.
string escape( string $val [, bool $not_qu )
val - string value; not_qu - disable adding quotes when true, default is false
$db
= DB::db();
$comment
=
$db``->escape(``$_POST``[``'comment'``]);
$db``->query(```"INSERT INTO
comments (
comment) VALUES ({$comment})"```);