JKUAT App database interface
Installs: 61
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 5
Forks: 0
pkg:composer/angrycoders/db
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ~4.6
This package is not auto-updated.
Last update: 2022-02-01 12:46:35 UTC
README
Database driver for angrycoders/system and ecosystem.
Install
Via Composer
$ composer require angrycoders/db
Get master branch update
$ composer require "angrycoders/db": "dev-master"
Get for a specific version
$ composer require "angrycoders/db": "1.0.0"
Usage
Make sure your server is running first.
require_once __DIR__ . '../vendor/autoload.php'; use AngryCoders\Db\Db; use AngryCoders\Db\DbException; $db = new Db();
Adding a new table
$db->createTable(tableName, fields)
Creates a new table in the database, if it does not exist already.
tableName(string): name of the tablefields(array): associative array describe the table's attributes. See below.
fields passed to createTable:
$fields = array( fieldName1 => array(type, size [, extraAttrb1, extraAttrb2, ... , ...]), fieldName2 => array(type, size [, extraAttrib1, extraAttrb2, ... , ...]), ...)
fieldName(string): name of the attributetype(constant): Required. Constant exposed by the library. Include:Db::DATETIMEDb::DOUBLEDb::FLOATDb::INTEGERDb::TEXTDb::VARCHAR
size(integer): Required. Length of field. If a field does not have a size just specify zero (0).extraAttrb1, extraAttrb2, ...: Optional. Any other attributes to be applied. For example,Db::AUTO_INCREMENTDb::NOT_NULLDb::PRIMARY_KEY
Example:
try { $db->createTable("student", array( "studentID" => array(Db::INTEGER, 11, Db::PRIMARY_KEY, Db::AUTO_INCREMENT), "regNo" => array(Db::VARCHAR, 20), "accountID" => array(Db::INTEGER, 11), "name" => array(Db::VARCHAR, 50))); } catch (DbException $e) { echo $e; }
Deleting a table
$db->deleteTable(tableName);
Removes a table from the database.
tableName(string): table name
Example:
$db->deleteTable("tableName");
Inserting a record
$db->insertRecord(tableName, values)
Inserts a record into the target table.
tableName(string): table namevalues(array): array of values to be inserted.
Example:
$db -> insertRecord("student", array(NULL, 'cs281-3722/2013', '54', '23', 'Magani Felix'));
Deleting a record
$db->deleteRecord(tableName, value, field)
Deletes a record from table tableName where field equals value.
tableName(string): name of tablevalue(Any): value to be compared withfield(string): name of field to be used to search forvalue
Example:
$db->deleteRecord("student", 'cs281-3722/2013', "regNo");
Selecting records
$db->getRecord(tableName, field, value [, targetFields])
Selects all records from table tableName where field field contains value.
tableName(string): table namefield(string): name of field to used to search forvaluevalue(Any): value to be compared withtargetFields(array): Optional. Array of field names to return values from. If not passed, all the fields will be used.
Example:
$result = $db->getRecord("student", 'name', 'Felix'); foreach($result as $row) { echo $row['studentID'] . "<br>"; echo $row['regNo'] . "<br>"; echo $row['courseID'] . "<br>"; echo $row['name'] . "<br>"; echo $row['accountID'] . "<br><br>"; } $result = $db->getRecord("student", 'name', 'Felix', array('name','accountID')); foreach($result as $row) { echo $row['name'] . "<br>"; echo $row['accountID'] . "<br>"; }
Getting records
$db->getAllRecords(tableName [, targetFields [, numOfRecords [, startIndex]]])
Return all records in table tableName.
tableName(string): table nametargetFields(array): Optional. Array of field names to return values from. If not passed, all the fields will be usednumOfRecords(integer): Optional. number of records to returnstartIndex(integer): Optional. From which index to fetch records from
Example:
$result = $db->getAllRecords("student"); foreach($result as $row) { echo $row['studentID'] . "<br>"; echo $row['regNo'] . "<br>"; echo $row['courseID'] . "<br>"; echo $row['name'] . "<br>"; echo $row['accountID'] . "<br><br>"; } //selecting accountID and name fields only to be returned $result = $db->getAllRecords("student", array('name','accountID')); //selecting the first 5 records to be returned $result = $db->getAllRecords("student", null, 5); //selecting the next 10 records to be returned from the 5th record $result = $db->getAllRecords("student", null, 10, 4);
Updating records
$db->updateRecord(tableName, fields, values, discriminantField, discriminantValue)
Update records in table tableName in the fields fields with the values values where the field discriminantField equals discriminantValue.
tableName(string): table namefields(array): array of table fields to targetvalues(array): array of values to update withdiscriminantField(string): name of field to use to compare records withdiscriminantValue(Any): value used to compare against
Example:
$tableName = "student"; //The name of the table $fields = array('name', 'accountID'); //Fields to be updated $values = array('John Doe', '23'); //The new values $field = 'regNo'; //The field to check $value = 'cs281'; // The value of the field to check $db->updateRecord($tableName, $fields, $values, $field, $value);
Testing
$ phpunit
Contributing
Please see CONTRIBUTING.md for details.
Security
If you discover any security related issues, please email keysindicet@gmail.com instead of using the issue tracker.
Credits
Magani Felix and Contributors.
License
The MIT License (MIT). Please see LICENSE.md for more information.