Startup

Technology Review

Tips

Video

Welcome

Home » Technology Review

PHP: Establish Class for MySQL Connections

Submitted by admin on Thursday, 20 November 2008No Comment

Basic Class for creating a database connection

Class MySQLConnection
{
// ———————–
// variable initialisation
// ———————–
var $SERVER = “hostname”; // Server name
var $USER = “dbusername”; // Database username
var $PWD = “dbpassword”; // Database password
var $DBNAME = “dbname”; // Database name
var $DBCONN = “”;
// —————————
// flag an error and terminate
// —————————
function db_error($strText)
{
$errno = mysql_errno();
$errmsgmsg = mysql_error();
echo “ERROR: $strText Error: ($errno) $errmsg )”;
exit;
}
// ——————————-
// attempt database initialisation
// ——————————-
function init_db ()
{
// create connection string and attempt connection
$dbconn = mysql_connect($this->SERVER,$this->USER,$this->PWD);
if (!$dbconn) { $this->db_error(”Database Connection Failed”); }
// attempt to open the chosen database
if (!mysql_select_db($this->DBASE,$dbconn)) { $this->error(”Unable To Select Database”); }
// make the connection available
$this->DBCONN = $dbconn;
}
} // Class MySQLConnection
?>

Go to Source

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.