One Class out of three that conform an ADODB like object system to work with MySQL. Connection provides the logging operations and database connection, selection and release.
By : Slainte
<?
// Base connection class for MySQL operations
// Full information at http://www.phpclasses.org/mysql_db
class mysql_conn {
// Init variables required
VAR $server = "" ;
VAR $user = "" ;
VAR $pwd = "" ;
VAR $database = "" ;
VAR $debuglv = true ;
// Data related variables
VAR $db = 0 ;
VAR $error_level = 0 ;
VAR $error_desc = "No errors" ;
VAR $logfile = "datalog" ;
VAR $filehdl = false ;
VAR $msg = "" ;
// Private methods
function connect_to_db()
{
$this->db = mysql_connect ($this->server,$this->user,$this->pwd) ;
$this->error_level = mysql_errno() ;
$this->error_desc = mysql_error() ;
if (!$this->db)
{
$this->msg = "\r\n" . date("d/m/Y - H:i:s") . " - ERROR " . $this->error_level . ": " . $this->error_desc ;
$this->debug() ;
} else {
mysql_select_db($this->database,$this->db) ;
$this->msg = "\r\n" . date("d/m/Y - H:i:s") . " - OPERATION O.K.: Connected to database " . $this->database ;
}
}
function release_db()
{
if ($this->db)
{
mysql_close($this->db) ;
$this->msg = "\r\n" . date("d/m/Y - H:i:s") . " - OPERATION O.K.: Database " . $this->database . " released";
} else {
$this->msg = "\r\n" . date("d/m/Y - H:i:s") . " - OPERATION FAILED: No database open";
}
$this->debug() ;
$this->logfile_close() ;
}
function logfile_init()
{
$fechagm = gmmktime()+3600 ;
$fecha = getdate($fechagm) ;
$this->logfile = $this->logfile . "-" . $fecha["mon"] . "-" . $fecha["year"] ;
$this->filehdl = fopen($this->logfile,'a') ;
if ($this->filehdl)
{
$this->msg = "\r\n" . date("d/m/Y - H:i:s") . " ===== SESSION STARTED BY " . $GLOBALS["PHP_SELF"] . " =====";
} else {
echo "<!-- UNABLE TO OPEN SPECIFIED LOG FILE " . $this->logfile . " -->" ;
}
$this->debug() ;
}
function logfile_close()
{
if ($this->filehdl)
{
fclose($this->filehdl) ;
}
}
function debug()
{
if ($this->debuglv) {
if ($this->filehdl)
{
fwrite($this->filehdl,$this->msg) ;
} else {
echo "<!-- ".$this->msg."-->" ;
}
}
}
// Public interface
function init()
{
$this->logfile_init() ;
$this->connect_to_db() ;
}
function destroy()
{
$this->release_db() ;
}
function mysql_conn($servername="localhost",$username,$password,$databasename,$debuglevel=true)
{
$this->server = $servername ;
$this->user = $username ;
$this->pwd = $password ;
$this->database = $databasename ;
$this->debuglv = $debuglevel ;
}
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Database Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
|
|
|
|
Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
|
|
|
|
Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
|
|
|
|
Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |