MySQL Connector class
Class developed to wrap up common native PHP mysql functions for use in projects from small to large.
This class makes it simple to use mysql in scripts as well as to aggregate these functions
into other classes that make up a project.
Please note this class is a work in progress and more functionality will be included in successive updates.
Getting started
1) Create a config file (or add to existing config file) using either of the following conventions:
Defined as Constants (Recommended)
-> define('HOST','localhost'); //Host your database server is on...most cases localhost is where the server is located, however check with your Host to make sure of this
-> define('USER','mysql_username'); //Your MySQL Username to access the database defined below
-> define('PASS','mysql_user_password'); //Your mySQL password
-> define('DATABASE','mysql_database'); //The database you want to work with
-> define('CTYPE','Connectiontype'); //Connection type to create, Non Persistent or persistent, 1 for non persistent (Default) and 2 for persistent
Defined as variables
-> $host = "localhost"; //Host your database server is on...most cases localhost is where the server is located, however check with your Host to make sure of this
-> $user = "mysql_username"; //Your MySQL Username to access the database defined below
-> $pass = "mysql_user_password"; //Your mySQL password
-> $database = "MySQL Database"; //The database you want to work with
-> $ctype = "Connectiontype"; //Connection type to create, Non Persistent or persistent, 1 for non persistent (Default) and 2 for persistent
2) Create an object name to use the class in an application page
a) If using constants in a config file:
-> $mysql = new mysqlconnection(HOST,USER,PASS,DATABASE,CTYPE); //if CTYPE is not included then defaults to 1 (Non Persistent)
b) If using variables in your config file:
-> $mysql = new mysqlconnector($host,$user,$pass,$database,$ctype); //if $ctype is not included then defaults to 1 (Non Persistent)
To create more then one connection to the database or to use another database at the same time
then create an object name for the connection with a number appended to it, also create
additional parameters in your config file similar to the above but with a number appended to
these as well:
Example:
-> $mysql = new mysqlconnection(HOST,USER,PASS,DATABASE,CTYPE); //Database connection 1
-> $mysql2 = new mysqlconnection(HOST,USER2,PASS2,DATABASE2,CTYPE2); //Database connection 2
3) Simple mysql connection example:
-----------------------------------
require_once "includes/config.inc.php"; //Require the config file
require_once "includes/mysql.class.php"; //Require the mysql connector class
$mysql = new mysqlconnector(HOST,USER,PASS,DATABASE); //Create connection to database
//Create a sql query
$sql = "SELECT * FROM example";
//Execute the query
$mysql->execute_query($sql);
//Check to see if the resultset has records
if($mysql->numRows() <= 0) {
print "no records found"; //no records were found
} else {
// print out each row using associative indices
while($row = $mysql->fetchrow(3)) {
print $row['field1'] ."<br />";
}
}
$mysql->closeDB();
-----------------------------------
This was just a simple script example, however you can also use this class aggregated in another
class just as easy.
Functions:
-> persistent or non persistent functions
-> execute_query - Used to execute a query on the database
-> fetchRow - returns the result set array, array can be in the form of associative and number indices or both (Default is both)
arguments for this function are: 1 - Both number and Associative Indices (default) 2 - As Number indices 3 - Associative indices
-> numRows - Returns the number of rows in a database query
-> affectedRows - Returns the number of rows affected by an INSERT, UPDATE or DELETE query
-> getInsertID - Returns the ID of the last inserted record in an INSERT query
-> errorMsg - Returns the error message if a query fails
-> closeDB - closes all non persistent Database connections, although this is not expressly required as PHP will close these connections when a page is loaded completely and all scripts have run
|