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!
|
|
|
|
Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. FREE! Go There Now!
|
|
|
|
IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects. FREE! Go There Now!
|
|
|
|
Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. 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!
|
|
|
|
This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. 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!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads. FREE! Go There Now!
|
|
|
|
You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
|
|
|
|
Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |