PHP: Establish Class for MySQL Connections
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
?>
Leave a comment!