So I'm trying to use this mysqli connection class (code below) but I am receiving the error message: Fatal error: Undefined class constant 'DBUSER' [...] I can't figure out why because I have set all the database connection credentials and included the config file.
My db.config.class.php:
class config {
public static $DBSERVER = "localhost"; // Set the IP or hostname of the database server you wish to connect to
public static $DBNAME = "**REMOVED**"; // Set the name of the database you wish to connect to
public static $DBUSER = "**REMOVED**"; // set the database user name you wish to use to connect to the database server
public static $DBPASSWORD = "**REMOVED**"; // set the password for the username above
public static $DBPORT = 3306;
public static $TABLEPREFIX = "";
}
mysqli.class.php:
include('db.config.class.php');
/**
* My-SQL database class
*
* @name mysql
* @version 2
* @author Leigh Edwards
* @category PHP
*/
class Dbconnect {
// leave blank if used for multiple users and call setUser method
private $sqlUser = "";
// leave blank if used for multiple users and call setPassword method
private $sqlPassword = "";
// set this to the database name you wish to use. If this class is used to access a number of Databases
// leave blank and call the select method to select the desired database
private $sqlDatabase = "";
// set this to the database server address. If you are using this class to connect to differant server
// leave blank and call the setHost method
private $sqlHost = "";
// Set this to the prefix of your tables if you set one while installing.
// default = ""
public $table_prefix = "";
private $result; // Query result
private $querycount; // Total queries executed
private $linkid;
/////////////////////////////////////////END CONFIG OPTIONS/////////////////////////////////////////////////////
function __construct() {
$this->loadDefaults ();
$this->connect ( $this->sqlHost, $this->sqlUser, $this->sqlPassword, $this->sqlDatabase );
$this->select ( $this->sqlDatabase );
}
/*
* method to load the object with the defaut settings
*/
private function loadDefaults() {
$this->sqlUser = config::DBUSER;
$this->sqlPassword = config::DBPASSWORD;
$this->sqlHost = config::DBSERVER;
$this->sqlDatabase = config::DBNAME;
$this->table_prefix = config::TABLEPREFIX;
}
public function getResult() {
return $this->result;
}
/**
* method to return the prefix for the sql tables
*
* @return = string $this->table_prefix
*/
public function get_tablePrefix() {
return $this->table_prefix;
}
/**
* function to return a string from within another string
* found between $beginning and $ending
*
* @param string $source
* @param string $beginning
* @param string $ending
* @param string $init_pos
*/
function get_middle($source, $beginning, $ending, $init_pos) {
$beginning_pos = strpos ( $source, $beginning, $init_pos );
$middle_pos = $beginning_pos + strlen ( $beginning );
$ending_pos = strpos ( $source, $ending, $beginning_pos + 1 );
$middle = substr ( $source, $middle_pos, $ending_pos - $middle_pos );
return $middle;
}
/**
* method to connect to the MySQL database server.
*
* @param string $sqlHost
* @param string $sqlUser
* @param string $sqlPassword
**/
function connect($sqlHost, $sqlUser, $sqlPassword, $sqlDatabase) {
try {
$this->linkid = mysqli_connect ( $sqlHost, $sqlUser, $sqlPassword, $sqlDatabase, 3306 );
if (! $this->linkid) {
die ( 'Connect Error (' . mysqli_connect_errno () . ') ' . mysqli_connect_error () );
}
} catch ( Exception $e ) {
die ( $e->getMessage () );
}
}
/**
* method to select the database to use
* @param string $sqlDatabase
*/
function select($sqlDatabase) {
try {
if (! @mysqli_select_db ( $sqlDatabase, $this->linkid )) {
throw new Exception ( "The Selected Database Can Not Be Found On the Database Server. $sqlDatabase (E2)" );
}
} catch ( Exception $e ) {
die ( $e->getMessage () );
}
}
/**
* method to query sql database
* take mysql query string
* returns false if no results or NULL result is returned by query
* if query action is not expected to return results eg delete
* returns false on sucess else returns result set
*
* NOTE: If you requier the the actual result set call one of the fetch methods
*
* @param string $query
* @return boolian true or false
*/
function query($query) {
// ensure clean results
unset ( $this->result );
// make query
$this->result = mysqli_query ( $query, $this->linkid );
if (! $this->result) {
echo "<br>Query faild: $query";
return FALSE;
} else {
return true;
}
}
/**
* method to return the number of rows affected by the
* last query exicuted
* @return int
*/
function affectedRows() {
$count = mysqli_affected_rows ( $this->linkid );
return $count;
}
/**
* method to return the number of rows in the result set
* returned by the last query
*/
function numRows() {
$count = @mysqli_num_rows ( $this->result );
return $count;
}
/**
* method to return the result row as an object
* @return object
*/
function fetchObject() {
$row = @mysqli_fetch_object ( $this->result );
return $row;
}
/**
* method to return the result row as an indexed array
* @return array
*/
function fetchRow() {
$row = @mysqli_fetch_row ( $this->result );
return $row;
}
/**
* method to return the result row as an associative array.
* @return array
**/
function fetchArray() {
$row = @mysqli_fetch_array ( $this->result, mysqli_ASSOC );
return $row;
}
/**
* method to return total number queries executed during
* the lifetime of this object.
*
* @return int
*/
function numQueries() {
return $this->querycount;
}
function setResult($resultSet) {
$this->result = $resultSet;
}
/**
* method to return the number of fields in a result set
* @return int
**/
function numberFields() {
return @mysqli_num_fields ( $this->result );
}
/**
* method to return a field name given an integer offset
* @return string
**/
function fieldName($offset) {
return @mysqli_field_name ( $this->result, $offset );
}
/**
* method to return the results of the last query
* in html table
*
* This method uses the $actions string to pass html code
* this is added to the table to enable display of images or links
* in the last columb of the table
*
* if boolian false is passed no html is add to the result table
* $startCol sets the col to start displaying 0 being the first
*
* @param int $startCol
* @param string or boolian false $actions
* @return string containing html code to dispay the table
*/
function getResultAsTable($startCol, $actions = "") {
if ($this->numrows () > 0) {
// Start the table
$resultHTML = "<table width=\"80%\" border=\"0\" align=\"center\" cellpadding=\"1\" cellspacing=\"0\"><tr>";
$resultHTML .= "<td><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\"><tr>";
$x = $startCol;
// Output the table header
$fieldCount = $this->numberFields ();
for($i = $x; $i < $fieldCount; $i ++) {
$rowName = $this->fieldName ( $i );
$resultHTML .= "<th align=\"left\">$rowName</th>";
}
if (! $actions === false) {
$resultHTML .= "<th align=\"left\">actions</th>";
}
$resultHTML .= "</tr>";
while ( $row = $this->fetchRow () ) {
$resultHTML .= "<tr>";
for($i = $x; $i < $fieldCount; $i ++)
$resultHTML .= "<td align=\"left\">" . htmlentities ( $row [$i] ) . "</td>";
if (! $actions === false) {
// Replace VALUE with the correct primary key
$action = str_replace ( "VALUE", $row [0], $actions );
$resultHTML .= "<td nowrap align=\"left\">$action</td>";
}
$resultHTML .= "</tr>";
}
$resultHTML .= "</table></td></tr></table>";
} else {
$resultHTML = "";
}
return $resultHTML;
}
/**
* method to retun the value of a given colum using one where clause
* @param $table
* @param $col
* @param $val1
* @param $col2
*/
function getRow($table, $col, $val1, $col2) {
$query = "SELECT '$col2' FROM " . $this->table_prefix . $table . " WHERE $col = '$val1'";
$this->query ( $query );
$resultArray = $this->fetchArray ();
return $resultArray [$col2];
}
/**
* method to test if a row conatining $x in the feild $y exists in the given $table
* method returns true or false
* @param $table
* @param $col
* @param $val
* @return boolian
*/
function rowExistsInDB($table, $col, $val) {
$this->query ( "SELECT $col FROM '" . $this->table_prefix . $table . "' WHERE '$col' = '$val'" );
if ($this->numRows () > 0) {
return true;
} else {
return false;
}
}
function rowExistsInDB2($table, $col, $val, $col2, $val2) {
$query = "SELECT " . $col . " FROM " . $this->table_prefix . $table . " WHERE " . $col . " = '" . mysqli_real_escape_string ( $val ) . "' AND " . $col2 . " = '" . mysqli_real_escape_string ( $val2 ) . "'";
$this->query ( $query );
if ($this->numRows () > 0) {
return true;
} else {
return false;
}
}
/**
* method to delete all rows where $col=$val in $table
* returns int of number of affected rows or false on fail
*
* @param string $table
* @param string $col
* @param string $val
* @return int
*/
function deleteRow($table, $col, $val) {
$this->query ( "DELETE FROM '" . $this->table_prefix . $table . "' WHERE '$col' = '$val'" );
return $this->result;
}
// Misc methods to do some convertions and stuff
// round or pad to 2 decimal points
function formatNum($num, $dec = 2) {
for($x = 0; $x <= 5; $x ++) {
$num = sprintf ( "%01." . ($dec + $x) . "f", $num );
return $num;
}
}
/**
* method to reverse the order of a given date
* and fix to mysql date format
* so DD/MM/YYYY becomes YYYY-MM-DD
*
* @param string $date
* @return string
*/
function revDate($date) {
// first split the date string @ / int o three parts
$dateArray = explode ( '/', $date, 3 );
// then reorder them to YYY-MM-DD
$revDate = array_reverse ( $dateArray );
$i = 0;
foreach ( $revDate as $eliment ) {
$correctDate .= $eliment;
if ($i < 2) {
$correctDate .= "-";
}
$i ++;
}
return $correctDate;
}
/**
* method to revers dates taken from sql database
* so YYYY-MM-DD becomes DD/MM/YYYY
*
* @param string $date
* @return string
*/
function revSqlDate($date) {
// first split the date string @ / int o three parts
$dateArray = explode ( '-', $date, 3 );
// then reorder them to DD/MM/YYYY
$revDate = array_reverse ( $dateArray );
$i = 0;
foreach ( $revDate as $eliment ) {
$correctDate .= $eliment;
if ($i < 2) {
$correctDate .= "/";
}
$i ++;
}
return $correctDate;
}
}
Write as:
config::$DBUSER;
and etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With