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).
- Perform a query such as Insert or Update
- Get a record count from the database
- Get a single row from the database
- 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.. |
|
|---|---|
|
Example 1
|
Example 2
|
|
Example 3
|
Example 4
|
|
Example 5
|
Example 6
|
|
Example 7
|
Example 8
|
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';
Usage Docs