UsageUsage Docs
Overview Overview
Introduction
Overview Quick Examples
Overview Installation
Include
Init


Valid XHTML 1.0 Transitional

mySQLEz v 1.5 Help and Docs

mySQLEz Overview

  • mySQLEz is a widget that makes it very fast and easy for you to use a mySQL database within your PHP script.
  • It is one php file that you include at the top of your script. Then, instead of using standard php database functions listed in the php manual, you use a much smaller (and easier) set of mySQLEz functions.
  • It automatically caches query results and allows you to use easy to understand functions to manipulate and extract them without causing extra server overhead
  • Most mySQLEz functions can return results as Objects, Associative Arrays, or Numerical Arrays
  • It automatically escapes all queries using mysql_real_escape_string
  • Inserts or update are done using the post array no need to write sql, just name each field the same as the table field
  • It can dramatically decrease development time and in most cases will streamline your code and make things run faster as well as making it very easy to debug and optimize your database queries.
  • It is a small class and will not add very much overhead to your website.

Note: It is assumed that you are familiar with PHP, basic Database concepts and basic SQL constructs. Even if you are a complete beginner mySQLEz can help you once you have read and understood this tutorial.


Introduction

When working with databases most of the time you will want to do one of four types of basic operations (CRUD).

  1. Perform a query such as Insert or Update
  2. Get a record count from the database
  3. Get a single row from the database
  4. Get a list of results from the database

mySQLEz wraps up these basic actions into easy to use functions.

  • mixed $db->doInsert(table, postVals)
  • mixed $db->doUpdate(table, keyColName, id, postVals)
  • mixed $db->doDelete(table, id)
  • mixed $db->doSQL(query)
  • mixed $db->fetchObj(query)
  • mixed $db->fetchArray(query, arrayType)
  • mixed $db->fecthRow(table, fields keyColName, id)
  • mixed $db->fecthCount(table, fields keyColName, id)

Quick Examples..
Note: In all these examples no other code is required other than including mysqlez.php

Example 1


		    
		    // Select multiple records from the database and print them out..
		    $users = $db->fetchObj( "SELECT name, email FROM users" );
		    foreach ( $users as $user )
		    {
			// Access data using object syntax
			echo $user->name;
			echo $user->email;
		    }
		    
		

Example 2


		    
			//Get Array from the database and print it out..
			$user = $db->fetcharray( "SELECT name,email FROM users WHERE id = 2", MYSQL_ASSOC );
			echo $user['name'];
			echo $user['email'];
		    
		

Example 3


		
		    // Get one Row from the database and print it out..
		    $user = $db->fetchRow( "users", "*", "id", 1 );
		    print_r( $user  );
		
	    

Example 4


		
		    // Insert into the database
		    $db->doInsert( "users", $_POST );
		
	        
	    

Example 5


		
		    // Update the database
		    $db->doUpdate( "users", "id", $id, $_POST );
		
	    

Example 6


		
		    // Query using standard sql
		   $list = $db->doSQL( "SELECT * FROM users LIMIT 1,10 );
		    foreach ( $list as $user )
		    {
		        echo $name;
		    }	        
	    

Example 7


		
		    // Display the structure and contents of any result(s) .. or any variable
		    $results = $db->getResults( "SELECT name, email FROM users" );
		    $db->varDump( $results );
		
	    

Example 8


		
		    // Get count (based on column index if needed)for pagination 
		    $count = $db->fetchCount( "users", "*" )
		    echo $count;
		    }
	        
	    

Installation

Include method

		
		    // Include mySQLEz Class
		    include_once "mysqlez.php";
		    // Initialize database object and establish a connection
		    // at the same time - db_user / db_password / db_name / db_host
		    $db = new mySQLEz('db_user','db_password','db_name','db_host');
		
	    

Note: On most systems localhost will be fine for the dbhost value. If you are unsure about any of the above settings you should contact your provider or look through your providers documentation.
If you are running on a local machine and have just installed mySQL for the first time, you can probably leave the user name and password empty ( i.e. = "" ) until you set up a mySQL user account.


Init method


Create a folder called inc and unzip mySQLEz into the inc folder, inside the folder create a file called config.php with the following lines

		
		    define( 'DB_HOST' , 'localhost' ); //edit if your hostname is different then localhost
		    define( 'DB_USER', 'db_username' ); //edit this to the username for your database
		    define( 'DB_PASS', 'db_password' ); //edit this to your database password
		    define( 'DB_NAME', 'db_name' ); //edit this to your database name
		
	    

Create a second file in the inc folder and call it init.php add the following to it

	    
		//require our conf setting for the db connection
		    require_once dirname(__FILE__) . '/config.php';
		// Include mySQLEz class
		    include_once dirname(__FILE__) . '/mysqlez.php';
		// Initialize database object and establish a connection
		// at the same time - db_user / db_password / db_name / db_host
		    $db = new mySQLEz(DB_USER,DB_PASS,DB_NAME,DB_HOST);
	    
	

At the top of your php or header files add:

	    
                include_once 'inc/init.php';