| px | top | add code | search | signup | login | help |
<?php
// You can change this to show no errors as we
// are collecting and showing them ourself.
error_reporting(0);
/**
* IdealMySQL, MySQL class
*
* This is a MySQL class for Idealws. This will
* be used in all projects that use a MySQL database.
* @author Ray Cuzzart II <ray@idealws.net>
* @version 1.0
* @package Idealws
* @copyright 2007 Idealws - All Right's Reserved
* @example test.php
*/
/**
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RAY CUZZART II, IDEAL WEB SERVICES OR ANY OTHER
* CONTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
* IdealMySQL class.
*
* This is the class file used to connect and do
* common things with a MySQL database.
* @package Idealws
* @subpackage classes
*/
class IdealMySQL {
/**
* @access protected
* @var string
* @desc Set to 1 to send errors to admins email address.
*/
protected $adminSendEmail = 0;
/**
* @access protected
* @var string
* @desc Set to admins email address for error notification.
*/
protected $adminEmail = "yourname@yourdomain.com";
/**
* @access protected
* @var string
* @desc Reply to address. Usually a email address you do not except emails to.
*/
protected $adminFromEmail = "noreply@yourdomain.com";
/**
* @access protected
* @var string
* @desc Subject line for admins error email.
*/
protected $adminSubject = "There has been a error with name of your program.";
// *********************************************************************************** //
// ***************** DO NOT CHANGE ANYTHING BELOW THIS LINE ************************** //
// ********************* UNLESS YOU KNOW WHAT YOUR DOING ***************************** //
// *********************************************************************************** //
/**
* @access protected
* @var integer|string
*/
protected $db;
/**
* @access protected
* @var integer|string
*/
protected $dbHost;
/**
* @access protected
* @var integer|string
*/
protected $dbUser;
/**
* @access protected
* @var integer|string
*/
protected $dbPass;
/**
* @access public
* @var integer|string
*/
public $dbConnect;
/**
* @access public
* @var string
*/
public $dbResults;
/**
* @access public
* @var string
*/
public $dbSelected;
/**
* @access public
* @var integer
*/
public $dbAffectedRows;
/**
* @access public
* @var integer
*/
public $dbNumberRows;
/**
* @access protected
* @var integer|string
*/
protected $errorCode;
/**
* @access private
* @var integer|string
*/
private $errorText;
/**
* Constructor sets up {$db $dbHost $dbUser $dbPass}
*/
function __construct($vardb, $vardbhost, $vardbuser, $vardbpass) {
// Lets make sure the database information provided does not
// have any illegal characters in it.
if($this->checkvar($vardb))$this->db = $vardb;
if($this->checkvar($vardbhost))$this->dbHost = $vardbhost;
if($this->checkvar($vardbuser))$this->dbUser = $vardbuser;
if($this->checkvar($vardbpass))$this->dbPass = $vardbpass;
// Lets try and make a database connection
$this->MySQLConnect();
}
/**
* Function to connect to database
*
* This function is used to connect to the database of
* choice. This function is publicly accesable
* @return integer
*/
protected function MySQLConnect() {
// We need to try and connect to the database from here.
try {
$conn = mysql_connect($this->dbHost,$this->dbUser,$this->dbPass);
if($conn) {
$this->dbConnect = $conn;
// Lets select the database we are going to be using
try {
if(mysql_select_db($this->db)) {
$this->dbSelected = 1;
}else{
throw new Exception($this->errorCode = 6);
}
}
catch(Exception $e) {
$line = $e->getLine()-3; // Get line number where exception was thrown and subtract to actual line of error
$errfile = $e->getFile(); // Get the file name and path of the error
echo $this->getMessageMap();
// We are going yo send the email from
// here so the user does not have to wait.
if($this->adminSendEmail) {
$this->emailAdmin($this->errorText, $line, $errfile);
}
exit;
}
}else{
throw new Exception($this->errorCode = 2);
}
}
catch(Exception $e) {
$line = $e->getLine()-4; // Get line number where exception was thrown and subtract to actual line of error
$errfile = $e->getFile(); // Get the file name and path of the error
echo $this->getMessageMap();
// We are going yo send the email from
// here so the user does not have to wait.
if($this->adminSendEmail) {
$this->emailAdmin($this->errorText, $line, $errfile);
}
exit;
}
}
/**
* Function to do database queries
*
* This funstion is to do actual database queries. It will return
* the affected rows if it is a INSERT, DELETE, UPDATE, REPLACE, DROP
* if it is a SELECT, SHOW, DESCRIBE or EXPLAIN it will return the
* resource.
*
* We have not tested the input for the query. You should check
* what is being passed for illegal characters prior to sending
* the query here.
* @return integer
* @return resource|string
*/
public function MySQLQuery($query) {
// Lets get the query information we are going to check
// to see if it is a INSERT, DELETE, UPDATE, REPLACE or DROP
if (eregi("(^INSERT|DELETE|UPDATE|REPLACE|DROP)", $query)) {
try {
// Let's do the query
$this->dbResults = mysql_query($query, $this->dbConnect);
$this->dbAffectedRows = mysql_affected_rows($this->dbConnect);
// Let's check to see how many rows are affected
if ($this->dbResults) {
// Return the affected rows
return $this->dbAffectedRows;
}else{
// If there is a problem we need to let someone know
throw new Exception($this->errorCode = 5);
}
}
// Now we catch the error and process it
catch(Exception $e) {
$line = $e->getLine()-8; // Get line number where exception was thrown and subtract to actual line of error
$errfile = $e->getFile(); // Get the file name and path of the error
echo $this->getMessageMap();
// We are going yo send the email from
// here so the user does not have to wait.
if($this->adminSendEmail) {
$this->emailAdmin($this->errorText, $line, $errfile);
}
exit;
}
// Lets get the query information we are going to check
// to see if it is a SELECT, SHOW, DESCRIBE or EXPLAIN
}elseif (eregi("(^SELECT|SHOW|DESCRIBE|EXPLAIN)", $query)) {
try {
// Let's do the query
$this->dbResults = mysql_query($query, $this->dbConnect);
$this->dbNumberRows = mysql_num_rows($this->dbResults);
// Let's check to see how many rows are affected
if ($this->dbNumberRows) {
// Return the results
return $this->dbResults;
}else{
// If there is a problem we need to let someone know
throw new Exception($this->errorCode = 5);
}
}
// Now we catch the error and process it
catch(Exception $e) {
$line = $e->getLine()-8; // Get line number where exception was thrown and subtract to actual line of error
$errfile = $e->getFile(); // Get the file name and path of the error
echo $this->getMessageMap();
// We are going yo send the email from
// here so the user does not have to wait.
if($this->adminSendEmail) {
$this->emailAdmin($this->errorText, $line, $errfile);
}
exit;
}
}
}
/**
* Function to test variables
*
* This function is used to test if variables have illegal characters.
* the only characters we want in the string ar a-z A-Z _- and 0-9 anything
* else will be considered illegal and throw a error message.
* @param string $var string to test
* @return integer
*/
public function checkvar($var) {
try {
if(eregi('^[a-zA-Z0-9_-]+$', $var)) {
return TRUE;
}else{
throw new Exception($this->errorCode = 1);
// return FALSE;
}
}
catch (Exception $e) {
$line = $e->getLine()-3; // Get line number where exception was thrown and subtract to actual line of error
$errfile = $e->getFile(); // Get the file name and path of the error
echo $this->getMessageMap();
// We are going yo send the email from
// here so the user does not have to wait.
if($this->adminSendEmail) {
$this->emailAdmin($this->errorText, $line, $errfile);
}
exit;
}
}
/**
* Function to get error codes
*
* This function is used to get the errors from the flat
* file we have setup for our error system.
* @return string
*/
protected function getMessageMap() {
$errors = file("errors.txt");
foreach($errors as $error) {
list($key,$value) = explode(",",$error,2);
$errorArray[$key] = $value;
}
// Assign just the error to a error variable
// so we can use it in the emails.
$this->errorText = $errorArray[$this->errorCode];
// Return the error so we can print it out for the
// user.
return "<br><center><div class=\"errortitle\">Application Error!</div><div class=\"error\">".$errorArray[$this->errorCode]."</div></center>";
}
/**
* Function to email admin if there is a error.
*
* This function is used to email errors as they occur to the
* admin of the site.
* @return string
*/
protected function emailAdmin($errorMessage, $errorLine, $errorFile) {
// Lets's try and send a email when there is a error.
try {
// Who is the email coming from?
$headers = "From: $this->adminFromEmail" . "\r\n";
// Here is the message. You can change this to bee however you want.
$message = "Application Error!\n\nThe following error occured: ".$errorMessage."\nLine Number: ".$errorLine."\nFile: ".$errorFile;
// Actually send the email out.
$mailsent = mail($this->adminEmail, $this->adminSubject, $message, $headers);
// If the email gets sent out then we are good.
if ($mailsent) {
return TRUE;
}else{
// If the email fails we need to produce another error.
throw new Exception($this->errorCode = 4);
}
}
// Let's show the error now that we have produced it.
catch(Exception $e) {
echo $this->getMessageMap();
exit;
}
}
}
?>
Error Text File "error.txt":
1,Invalid characters in information provided!
2,Unable to connect to database.
3,Invalid email address please enter a proper email address and try again!
4,Unable to send email to admin!
5,Unable to process database query. Please try again.
6,Unable to select database.
Stylesheet:
.errortitle {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
font-size: 11px;
font-weight: bold;
border-top: 1px solid #68686A;
border-right: 1px solid #68686A;
border-left: 1px solid #68686A;
width: 400px;
padding: 4px 4px 4px 4px;
background-color: #C00000;
color: White;
text-align: left;
}
.error {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
font-size: 11px;
font-weight: normal;
border: 1px solid #68686A;
background-color: #FFFFFF;
padding: 4px 4px 4px 4px;
width: 400px;
text-align: left;
background-image: url(error.png);
background-repeat: no-repeat;
height: 75px;
padding-left: 65PX;
background-position: left;
}
.center {
vertical-align: middle;
}
Test Example File test.php:
<html>
<head>
<title>Test IdealMySQL Class</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
// Include the IdealMySQL Class
include ("./IdealMySQL.class.php");
// Create a new object
$dbtest = new IdealMySQL("database","localhost","dbuser","dbpassword");
// Check variable to make sure it does not have any invalid characters
// the only valid characters are: a-zA-Z0-9_-
$dbtest->checkvar("a$");
// Insert some information into the database.
$dbtest->MySQLQuery("INSERT INTO database(name,email,text) VALUES('name','email@email.com','This stuff')");
// Selet some information from the database
$stuff = $dbtest->MySQLQuery("SELECT * FROM database");
while($row = mysql_fetch_array($stuff)) {
echo $row['id']."<br>";
}
?>
</body>
</html>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.