Sunday, April 6, 2014

PHP -MySQLCrank



MySQLCrank

Provides a number of functions to make it easier to work with MySQL databases, without using a lot of mysqli_xxxx functions, making it easier to switch to a different database. Supports SELECT, INSERT, UPDATE, and DELETE queries only, first because of security reasons, second - other queries are just not really needed for web development. 

Using MySqli_ extension (as described in php.net):

The mysqli extension, or as it is sometimes known, the MySQL improved extension, was developed to take advantage of new features found in MySQL systems versions 4.1.3 and newer. The mysqli extension is included with PHP versions 5 and later. The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:
  • Object-oriented interface
  • Support for Prepared Statements
  • Support for Multiple Statements
  • Support for Transactions

Supporting Parameters :

Through the support of the parameters you can use the various operations on the database in the same format, you can only change the type of operation required.

Classes

  • TDatabase
  • Includes all database operations function like SELECT , INSERT , UPDATE and DELETE .
  • TSQL
  • Includes structure of Parameters.

Setup database configuration

from TDatabase Class find these line and configure it as your database config.

$this->db_host "__SERVER_" ;$this->db_username "__DATABASE_USERNAME__";$this->db_password "__DATABASE_PASSWORD__";$this->db_database "__DATABASE_NAME__";?>

Basic Usage

Creating new instance of TDatabase class .

require_once(
"includes/TDatabase.php");$db = new TDatabase() ; /* Create new instance */
unset(
$db);  /* unset when finish using it. */
?>

Basic Select

Using Basic Select operation without any conditions.

$db = new TDatabase() ;$result $db->select("movies") ;
unset(
$db);?>

Select with where clause

Using Basic Select operation without any conditions

$db = new TDatabase() ;$db->where("movie_year","2013","i") ;$db->where("movie_rate","8","i") ;$result $db->select("movies") ;
unset(
$db);?>

where clause



/* where($name,$value,$type,$operand="=",$nextCondition="and") */
$db = new TDatabase() ;$db->where("movie_year","2013","i") ;$db->where("movie_rate","8","i") ;$result $db->select("movies") ;
unset(
$db);?>

Select with where and like operand



/* where($name,$value,$type,$operand="=",$nextCondition="and") */
$db = new TDatabase() ;$db->where("movie_name","Trek","s","like") ; $result $db->select("movies") ;
unset(
$db);?>

Select count for paging results



$db = new TDatabase() ;$db->where("movie_name","Trek","s","like") ;$resultCount $db->selectCount("movies") ; /* get count for same criterias*/
$result $db->select("movies") ;
unset(
$db);?>

Select and Limits



$db = new TDatabase() ;$db->limit(0,10);$result $db->select("movies") ;
unset(
$db);?>

Select and Order



$db = new TDatabase() ;$db->order("order by id desc");$result $db->select("movies") ;
unset(
$db);?>

Select with specified Columns

In case parameters added in select mode ,will be considered as columns must show only.

$db = new TDatabase() ;$db->addParameter("movie_rate","","i"); /* Parameters in select clause used as columns  */
$db->addParameter("movie_year","","i");$db->order("order by id desc");$result $db->select("movies") ;
unset(
$db);?>

Insert Operation



$db = new TDatabase() ;$db->addParameter("movie_name","Frozen (2013)","s");$db->addParameter("movie_rate","8","i");$db->addParameter("movie_year","2013","i");$db->addParameter("movie_story","Movie Story Here","s");$rows_affected $db->insert("movies") ;
unset(
$db);?>

Insert if not found and delete if found

An example of how easy to execute multi operations at the same time.

$db = new TDatabase() ;$db->addParameter("movie_name","Movie name","s");$db->addParameter("movie_rate","9","i");$db->addParameter("movie_year","2014","i");$db->where("movie_name","Movie name","s") ;

if(
$db->selectCount("movies")<=0)$db->insert("movies");
else
$db->delete("movies");

unset(
$db);?>

Update Operation with where



$db = new TDatabase() ;$db->addParameter("movie_rate","8","i");$db->addParameter("movie_year","2013","i");$db->where("id","1","i") ;$rows_affected $db->update("movies") ;
unset(
$db);?>

Delete Operation with where



$db = new TDatabase() ;$db->where("id","1","i") ; $rows_affected $db->delete("movies") ;
unset(
$db);?>

Safe Mode

You can activate safe mode to avoid overall effect on the records in operations (delete, modify) ,in this mode you must add one criteria at least .

$db = new TDatabase() ;$db->setSafeMode(true);$db->delete("movies") ;
unset(
$db);?>

Debug Query

You can see the query executed in database by calling $db->getLastQuery();

$db = new TDatabase() ;$db->select("movies") ;
echo  
$db->getLastQuery();
unset(
$db);?>

Download now ...

0 التعليقات:

Post a Comment