|
Server : Apache/2.4.62 System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64 User : www ( 80) PHP Version : 8.3.8 Disable Function : NONE Directory : /domains/owens.enteract/inc/libs/db/ |
Upload File : |
<?php
/**
* Global util to manage open/closing db conn.
* If there's multiple DBs necessary consider splitting with a separate API/project.
*
*/
class DBConn{
public static $dbc;
public static $isOpen = false;
public static function open(){
if (DBConn::$isOpen){
return DBConn::$dbc;
}else{
$dbErrorReport = false;
if ($dbErrorReport){
// Send email error log... not great when testing local
DBConn::$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASS) or error_log(ROOT_LOCATION . '\n\nI cannot connect to the database because: ' . mysqli_error(DBConn::$dbc), 1,ERROR_EMAIL);
mysqli_select_db (DBConn::$dbc,DB_NAME) or error_log(ROOT_LOCATION . '\n\nI cannot connect to the database because: ' . mysqli_error(DBConn::$dbc), 1,ERROR_EMAIL);
}else{
// Connect or fail
DBConn::$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASS) or die ('I cannot connect to the database because: ' . mysqli_error(DBConn::$dbc));
mysqli_select_db (DBConn::$dbc,DB_NAME) or die ('I cannot connect to the database because: ' . mysqli_error(DBConn::$dbc));
}
mysqli_query(DBConn::$dbc,"SET NAMES utf8mb4");
mysqli_query(DBConn::$dbc,"SET CHARACTER SET utf8mb4");
DBConn::$isOpen = true;
return DBConn::$dbc;
}
}
public static function close(){
if (DBConn::$isOpen){
mysqli_close(DBConn::$dbc);
}
DBConn::$isOpen = false;
}
}
?>