PHP Code:
<?php
class Database {
private $sql;
private $database;
function __construct($customString = null, $mssqlCredentialsArray = false) {
if($mssqlCredentialsArray){
$this->sql = odbc_connect('Driver={SQL Server};Server=' . $mssqlCredentialsArray['Host'] . '\SQLEXPRESS;Port=1433',
$mssqlCredentialsArray['User'],
$mssqlCredentialsArray['Pass']);
}else{
$this->sql = $customString;
}
$this->database = array(
'Account' => 'ACCOUNT_DBF',
'Character' => 'CHARACTER_01_DBF',
'Item' => 'ITEM_DBF',
'Ranking' => 'RANKING_DBF',
'Log' => 'LOGGING_01_DBF',
'Website' => 'WEBSITE_DBF'
);
}
private function InputCheck($mixedInput){
foreach($mixedInput as $key => $value){
if(gettype($value) != 'integer'){
$mixedInput[$key] = htmlspecialchars($value);
$mixedInput[$key] = htmlentities($value);
$mixedInput[$key] = strip_tags($value);
$mixedInput[$key] = '0x'.bin2hex($value);
}else {
$mixedInput[$key] = $value;
}
}
return $mixedInput;
}
private function QueryConstruction($escapedInput, $queryType){
$queryRows = null;
$whereRows = false;
foreach($escapedInput as $key => $value){
$queryRows .= '[' . $key . '], ';
}
switch($queryType){
case 'rows':
if(gettype(key($escapedInput)) == 'integer'){
foreach($escapedInput as $key => $value){
$queryRows .= '[' . $value . '], ';
}
}else{
foreach($escapedInput as $key => $value){
$queryRows .= '[' . $key . '], ';
}
}
$subQueryRows = substr($queryRows, 0, strlen($queryRows) - 2);
break;
case 'dual':
foreach($escapedInput as $key => $value){
if(gettype($value) == 'integer'){
$queryRows .= '[' . $key . '] = ' . $value . ' AND ';
}else {
$queryRows .= '[' . $key . '] = \'' . $value . '\' AND ';
}
}
$subQueryRows = substr($queryRows, 0, strlen($queryRows) - 5);
$subQueryRows = ' WHERE ' . $subQueryRows;
case 'values':
foreach($escapedInput as $key => $value){
if(gettype($value) == 'integer'){
$queryRows .= $value . ', ';
}else {
$queryRows .= '\'' . $value . '\', ';
}
}
$subQueryRows = substr($queryRows, 0, strlen($queryRows) - 2);
break;
}
return $subQueryRows;
}
private function Exec($preparedQuery){
return @odbc_exec($preparedQuery);
}
private function Fetch($result){
return @odbc_fetch_array($result, 0);
}
private function Rows($result){
return @odbc_num_rows($result);
}
/**************************************************************************************************************************************************************************
Query Functions
*************************************************************************************************************************************************************************/
protected function Update($mixedInput, $usedDatabase, $table){
$queryCredentials = self::QueryConstruction(self::InputCheck($mixedInput, 'dual'));
$exec = 'UPDATE [' . $this->database[$usedDatabase] . '].[dbo].[' . $table . '] SET ';
}
protected function Insert($mixedInput, $usedDatabase, $table){
$queryCredentials = self::QueryConstruction(self::InputCheck($mixedInput, 'rows'));
$valueCredentials = self::QueryConstruction(self::InputCheck($mixedInput, 'values'));
$exec = 'INSERT INTO [' . $this->database[$usedDatabase] . '].[dbo].[' . $table . '] (' . $queryCredentials . ') VALUES (' . $valueCredentials . ')';
}
protected function Select($mixedInput, $usedDatabase, $table, $mixedWhere = false){
$queryCredentials = self::QueryConstruction(self::InputCheck($mixedInput, 'rows'));
if($mixedWhere){
$whereCredentials = self::QueryConstruction(self::InputCheck($mixedWhere, 'dual'));
}else { $whereCredentials = null; }
$craft = 'SELECT ' . $queryCredentials . ' FROM [' . $this->database[$usedDatabase] . '].[dbo].[' . $table . ']' . $whereCredentials . '';
$exec = self::Exec($craft);
$fetch = self::Fetch($exec);
$rows = self::Rows($exec);
return array('Result' => $fetch, 'Rows' => $rows);
}
}