This package is a MySQL database access wrapper with data manipulation functions.
There is one base class that can establish MySQL database connections, sanitize values to use in SQL queries, execute SQL queries, retrieve SELECT query results into arrays, execute SQL INSERT queries and retrieve the last inserted identifier.
There is also a sub-class that can retrieve the list of fields of a table, compose and execute an SQL SELECT, INSERT, UPDATE and DELETE queries from a list of parameters.
Steps:
1. Configure db info @ folder ("conf/config.php").
2. Connect to db.
eg. $dbconn = new MySQLDB($conf['dbhost'], $conf['dbusername'], $conf['dbpassword'], $conf['dbname']);
3. Instantiate table.
eg. $my_data = new Data('my_table_name');
***************************************
Adding Data Sample:
***************************************
@params: array of table fields
@params: array of form name and its value
$my_data->addData(array('field1', 'field2', 'field3'), array('field1'=>'value1', 'field2'=>'value2', 'field3'=>'value3'));
***************************************
Updating Data Sample:
***************************************
@params: array of table fields
@params: array of form name and its value
@params: array or string of condition
$my_data->updateData(array('field1', 'field2', 'field3'), array('field1'=>'value1', 'field2'=>'value2', 'field3'=>'value3'), array('field1'=>1));
***************************************
Deleting Data Sample:
***************************************
@param: array or string of condition
$my_data->deleteData(array('field1'=>1));
$my_data->deleteData(array("field1=1 AND field2=2"));
***************************************
Retrieving more than one data sample:
***************************************
--------------------------------------
@params: array of table fields
$my_data->getAllData(array('field1', 'field2', 'field3'));
--------------------------------------
@params: array of table fields
@params: arrays conditions
$my_data->getAllData(array('field1', 'field2', 'field3'), array( array(TRUE, 'AND', array('field1'=>'value1')) ), array(TRUE, '', array('field2'=>'value2')) ) );
--------------------------------------
@params: array of table fields
@params: arrays conditions
@params: array of ordering/sorting
$my_data->getAllData(array('field1', 'field2', 'field3'), array( array(TRUE, '', array('field2'=>'value2')) ), array('field1'=>'ASC', 'field2'=>'DESC') );
--------------------------------------
@params: array of table fields
@params: arrays conditions
@params: array of ordering/sorting
@params: int starting row of query
@params: int limitation of query
$my_data->getAllData(array('field1', 'field2', 'field3'), array( array(TRUE, '', array('field2'=>'value2')) ), array('field1'=>'ASC', 'field2'=>'DESC'), 0, 10 );
***************************************
Notes:
***************************************
To dynamically get array of table fields from specified table name:
$fields = $my_data->getFields();
To run your own query:
After instantiating db connection, eg. $dbconn = new MySQLDB($conf['dbhost'], $conf['dbusername'], $conf['dbpassword'], $conf['dbname']);
Then
@params: your query
@params: result type - options: "ARRAY" = more than one row, "ROW" = only one row
@params: query type - options 0 = MYSQL_BOTH, 1 = MYSQL_NUM, default 2 = MYSQL_ASSOC
$dbconn->MySQLDB_GetData("SELECT * FROM tbl", "ARRAY", 0)
By: Mitchelle C. Pascual (mitch.pascual at gmail dot com)
http://ordinarywebguy.wordpress.com
|