PHP Classes

File: sql.php

Recommend this page to a friend!
  Classes of philip njuguna   MySQL Connection Exceptions   sql.php   Download  
File: sql.php
Role: Class source
Content type: text/plain
Description: conn
Class: MySQL Connection Exceptions
Connect to MySQL and throw exceptions on error
Author: By
Last change:
Date: 8 years ago
Size: 1,048 bytes
 

Contents

Class file image Download
<?php

class ConnException extends Exception{}
class
Conn{
     protected
$host;
     protected
$username;
     protected
$password;
     protected
$db;

public function
_construct($host_,$username_,$password_,$db_)
{

       
$this->_host = $host_;
       
$this->_password= $password_;
       
$this->_username=$root_;
       
$this->_db=$db_;
   
        }
       
public function
connect($host_,$username_,$password_,$db_){
        if (
strlen($host_) == 0 || strlen($username_) == 0 || strlen($db_) == 0 ) {
      throw new
ConnException ('Please supply the '.$host_ .' name , '.$username_ .' Username and '.$db_ .'database name');
    }
       
$con=mysql_connect($host_, $username_, $password_);
       
       
mysql_select_db($db_);
        }
}
//Connecting to database
$conf= new Conn;
       
$host_='';//host name
       
$password_='';//password
       
$username_='';//username
       
$db_=''; //database name
       
try
{

$conf->connect($host_,$username_,$password_,$db_);
}
catch (
connException $e )
{
  echo
"Encountered an error while connecting to database: ";
}
?>