Showing posts with label mysql connection class. Show all posts
Showing posts with label mysql connection class. Show all posts

Wednesday, August 28, 2019

MySqli Connection Class in PHP

Here below is given the Connection class


class Database
{
var $sServer;
var $sDatabase;
var $sUsername;
var $sPassword;

var $dbConnection;
var $dbResultSet;

var $iCount;
var $iFieldsCount;
var $iAutoNumber;
var $sError;

function Database( )
{
$this->sServer   = DB_SERVER;
$this->sDatabase = DB_NAME;
$this->sUsername = DB_USER;
$this->sPassword = DB_PASSWORD;

$this->dbConnection = NULL;
$this->dbResultSet = NULL;

$this->iCount = 0;
$this->iAutoNumber = 0;
$this->sError = NULL;

if (!$this->dbConnection)
$this->connect( );
}

function connect( )
{
$this->dbConnection = @mysqli_connect($this->sServer, $this->sUsername, $this->sPassword, $this->sDatabase);

if (!$this->dbConnection)
{
  print "Error: Unable to connect to the database Server.";
  exit( );
}


if (!@mysqli_select_db($this->dbConnection, $this->sDatabase))
{
print "Error: Unable to locate the Database.";
  exit( );
}
}


function query($sQuery, $bFlag = false)
{
@mysqli_free_result($this->dbResultSet);

$this->dbResultSet = @mysqli_query($this->dbConnection, $sQuery);

if (!$this->dbResultSet)
{
$this->sError       = @mysqli_error($this->dbConnection);
$this->iCount       = 0;
$this->iFieldsCount = 0;

return false;
}

else
{
$this->iCount       = @mysqli_num_rows($this->dbResultSet);
$this->iFieldsCount = @mysqli_num_fields($this->dbResultSet);

return true;
}

}


function getCount( )
{
return $this->iCount;
}


function getField($iIndex, $sField)
{

if ($this->dbResultSet === false)
return false;

if ($iIndex >= @mysqli_num_rows($this->dbResultSet))
return false;

if (@is_string($sField) && !(@strpos($sField, ".") === false))
{
$sTableField  = @explode(".", $sField);
$sField       = -1;
$sTableFields = @mysqli_fetch_fields($this->dbResultSet);

for ($sRowId = 0; $sRowId < @mysqli_num_fields($this->dbResultSet); $sRowId++)
{
if ($sTableFields[$sRowId]->table == $sTableField[0] && $sTableFields[$sRowId]->name == $sTableField[1])
{
$sField = $sRowId;
break;
}
}

if ($sField == -1)
return false;
}

@mysqli_data_seek($this->dbResultSet, $iIndex);

$sDbLine = @mysqli_fetch_array($this->dbResultSet);

return isset($sDbLine[$sField])? $sDbLine[$sField] : false;

}


function execute($sQuery, $bFlag = true)
{
            @mysqli_free_result($this->dbResultSet);

            if (!@mysqli_query($this->dbConnection, $sQuery))
            {
                $this->sError       = @mysqli_error($this->dbConnection);
                $this->iCount       = 0;
                $this->iFieldsCount = 0;

                return false;
            }

            else
            {
                $this->iAutoNumber  = @mysqli_insert_id($this->dbConnection);
                $this->iCount       = 0;
                $this->iFieldsCount = 0;
            }

return true;
}


function close( )
{
@mysqli_free_result($this->dbResultSet);
@mysqli_close($this->dbConnection);
}


function error( )
{
return $this->sError;
}
}