PHP Classes

DB Manager Logger: Execute and log MySQL queries using PDO

Recommend this page to a friend!
  Info   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 339 All time: 7,080 This week: 455Up
Version License PHP version Categories
db-manager-logger 2.2.3GNU General Publi...5.4PHP 5, Databases, Logging
Description 

Author

This package can compose and execute MySQL queries using PDO.

It provides several functions to compose SQL SELECT, INSERT, UPDATE and DELETE queries from given parameter values that define table names, field names and values, conditions, etc..

The main class can also manage start, commit and revert transactions.

A separate class can log the executed database operations.

Picture of Victor Henrique Pereira
  Performance   Level  

 

Details

Database Manager

<p>This class provides a safe connection with database (yeah, it does use transaction) and log all actions performed daily into a single file/day.</p> <p>You can audit all queries was performed into database, with values, datetime and totally debugged by viewing the files created into ./log/ folder.</p>

Total Downloads Latest Unstable Version Latest Stable Version License

License

<p>This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.</p>

<p>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.</p>

<p>You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</p>

Installation

Execute the sql file located in `./src/`

Install [apache/log4php] using [composer].

To install, add `apache/log4php` to your `composer.json` file.

{
   "require" : {
    "apache/log4php": "2.3.0"
  },
}

From there, use the `composer install` or `composer update` commands to install.

Basic Usage

<?php
define('DB_HOST', 'my-host'); // define the host of mysql server
define('DB_PORT', 3306); // define the port of mysql server
define('DB_USER', 'my-user'); // define the user of mysql server
define('DB_PASS', 'my-pass'); // define the password of mysql server
define('DB_NAME', 'my-db'); // define the database name
define('DB_ENCODING', 'utf8'); // define the encoding of statements

// Assuming you installed from Composer:
require "vendor/autoload.php";

// Get the instance of DBManager:
$pdo = DBManager::getInstance();

###### INSERT STATEMENT ######
// Array with key->val pairs (columns that will be created)
$paramIns = array(
    'cn1' => 'cv1',
    'cn2' => 'cv2',
    'cnN' => 'cvN',
);
// Creates the sql
$insert = $pdo->createInsert('tableName', $paramIns);
/*
Output: INSERT INTO tableName(cn1,cn2,cnN) VALUES (:cn1,:cn2,:cnN);
*/
$resultInsert = $pdo->query($insert);

###### UPDATE STATEMENT ######
// Array with key->val pairs (columns that will be updated)
$paramUpd = array(
    'cn1' => 'cv1',
    'cn2' => 'cv2'
);
// Array with columnKey => val (columns that will be used on WHERE clause)
// If null or empty, the result sql will be "UPDATE tableName SET col1 = :val1, col2 = :va2, ... colN = :valN ;"
$paramCond = array(
	// The first parameter of array data is the where clause (like, equal, less, etc... [see the main class constants]])
	// The second parameter of second array is the operand type (and|or) to concat with next column. Use null if the last param
    'ck1' => array(DBManager::COL_EQUAL => array('cv1' => 'and')),
    'ck2' => array(DBManager::COL_EQUAL => array('cv2' => null))
);
// Creates the sql
$update = $pdo->createUpdate('tableName', $paramUpd, $paramCond);
/*
Output: UPDATE tableName SET `cn1` = :cn1, `cn2` = :cn2 WHERE `ck1` = :ck1 and `ck2` =  :ck2 ;
*/
$resultUpdate = $pdo->query($update);

###### DELETE STATEMENT ######
// Array with key->val pairs to where clause to delete
// If null or empty, the result sql will be "DELETE FROM tableName;"
$paramDel = array(
	// The first parameter of array data is the where clause (like, equal, less, etc... [see the main class constants]])
	// The second parameter of second array is the operand type (and|or) to concat with next column. Use null if the last param
	'ck1' => array(DBManager::COL_EQUAL => array('cv1' => null))
);
$delete = $pdo->createDelete('tableName', $paramDel);
/*
Output: DELETE FROM table WHERE  `key1` =  :key1;
*/
$resultDelete = $pdo->query($delete);

###### SELECT STATEMENT ######
// Array with columns to retrieve
$paramSelect = array('cn1', 'cnN');
// Array with columnKey => value to where clause to select
// If null or empty, the result sql will be "SELECT col1, col2,... colN FROM tableName ORDER BY col1,col2,...colN;"
$paramWhere = array(
	// The first parameter of array data is the where clause (like, equal, less, etc... [see the main class constants]])
	// The second parameter of second array is the operand type (and|or) to concat with next column. Use null if the last param
    'cn1' => array(DBManager::COL_EQUAL => array('cv1' => 'and')),
    'cn2' => array(DBManager::COL_LIKE => array('cv2' => null))
);
// Array with columnKey -> value to order clause to select
// If null or empty, the result sql will be "SELECT col1, col2,... colN FROM tableName;"
$paramOrder = array(
	// Fields used in order
    'fields' => array('cn1', 'cn2'),
	// Order type (asc|desc)
    'order' => 'ASC'
);
// Creates the sql
$select = $pdo->createSelect('tableName', $paramSelect, $paramWhere, $paramOrder);
/*
Output: SELECT `cn1`, `cnN` FROM table WHERE `cn1` = :cn1 and `cn2` LIKE :cn2 ORDER BY cn1,cn2 ASC;
*/  
$resultSelect = $pdo->select($select);  
  
unset($pdo);  
?>  

Screenshots

Logger HTML view Logger FILE view

[apache/log4php]:http://logging.apache.org/log4php/download.html [composer]:http://getcomposer.org/


Screenshots (1)  
  • Screenshot_2.png
  Files folder image Files (84)  
File Role Description
Files folder imagephpdoc (2 files, 9 directories)
Files folder imagesrc (2 files, 4 directories)
Files folder imagevendor (1 file, 2 directories)
Files folder imageviews (2 files)
Accessible without login Plain text file composer.json Conf. Composer dependencies
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file config.xml Data Config for the logger
Accessible without login Plain text file index.php Example Index page of testing class
Accessible without login Plain text file LICENSE Lic. Auxiliary data
Accessible without login Plain text file README.md Doc. Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 96%
Total:339
This week:0
All time:7,080
This week:455Up