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! |
Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. FREE! Go There Now!
|
|
|
|
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. 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!
|
|
|
|
Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |