/** * Constructor */ public function __construct() { $connectionInfo = array("UID" => DBSettings::$dbUser, "PWD" => DBSettings::$dbPass, "Database" => DBSettings::$database, "ReturnDatesAsStrings" => true); $this->_connection = sqlsrv_connect(DBSettings::$Server, $connectionInfo); // Creates and opens a connection. if ($this->_connection == null) { trigger_error('Failed to connect to SQL: ' . dbGetErrorMsg()); } }
function storePlayer($name, $email, $gcm_regid) { $db = new BaseDB(); // insert user into database $sql = "INSERT INTO gcm_users(name, email, gcm_regid, created_at) VALUES('{$name}', '{$email}', '{$gcm_regid}', GETDATE())"; $result = $db->dbQuery($sql); // check for successful store if ($result) { // get user details $id = $db->getLastId('gcm_users'); // last inserted id $result = $db->dbQuery("SELECT * FROM gcm_users WHERE id = {$id}") or die(dbGetErrorMsg()); // return user details $NumRecords = sqlsrv_num_rows($result); if ($NumRecords > 0) { return sqlsrv_fetch_array($result, SQLSRV_FETCH_BOTH); } else { return false; } } else { return false; } }
function insertRecord($text) { global $resultMessage; global $db; $sql = "\n\t\t\tSELECT count(*) AS counter FROM HeaderNames WHERE HeaderName = '{$text}'\n\t\t"; $records = $db->dbQuery($sql); if ($records) { $row = sqlsrv_fetch_array($records, SQLSRV_FETCH_ASSOC); if ($row['counter'] == '0') { // Headername does not exist yet // Insert record echo 'NEW HEADER: ' . $text . '<br>'; $sql = "\n\t\t\t\tINSERT INTO HeaderNames (HeaderName) VALUES ('{$text}');\n\t\t\t"; $result = $db->dbQuery($sql); if ($result == false) { $resultMessage = 'ERROR: ' . dbGetErrorMsg(); $db->dbTransactionRollback(); } } } else { $resultMessage = 'ERROR: ' . dbGetErrorMsg(); $db->dbTransactionRollback(); } }
/** * @param $record : A $Branch array * * @return array: An array of errors or empty if no errors * NB!!! * Note: empty array is converted to null by non-strict equal '==' comparison. * Use is_null() or '===' if there is possible of getting empty array. */ public function insert($record) { // Populate the Branch array if (!is_array($record)) { return array(printf('A Branch record was expected as a parameter but a %s was received', gettype($record))); } $sqlCommand = sprintf("\n\t\t\t\tBEGIN\n\t INSERT INTO [dbo].[Branch] (\n\t [BranchCode]\n\t ,[Name]\n\t ,[Active]\n\t ,[CustomMessage]\n\t ,[PhoneNumber]\n\t ,[FaxNumber]\n\t ,[PhysicalAddressLine1]\n\t ,[PhysicalAddressLine2]\n\t ,[PhysicalAddressLine3]\n\t ,[PhysicalAddressLine4]\n\t ,[PhysicalAddressLine5]\n\t ,[PostalAddressLine1]\n\t ,[PostalAddressLine2]\n\t ,[PostalAddressLine3]\n\t ,[PostalAddressLine4]\n\t ,[PostalAddressLine5]\n\t ,[BankName]\n\t ,[BankBranchName]\n\t ,[BankBranchCode]\n\t ,[BankAccountNumber]\n\t ,[ContactPersonName]\n\t ,[ContactPersonNumber]\n\t ,[ContactPersonEmail]\n\t ,[AdminContactPersonName]\n\t ,[AdminContactPersonNumber]\n\t ,[AdminContactPersonEmail]\n\t ,[Longitude]\n\t ,[Latitude]\n\t ,[BusinessEntityId] )\n\t VALUES (\n\t '%s',\n\t '%s',\n\t %d,\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t '%s',\n\t %f,\n\t %f,\n\t %d )\n END", $record['BranchCode']['Value'], $record['Name']['Value'], $record['Active']['Value'], $record['CustomMessage']['Value'], $record['PhoneNumber']['Value'], $record['FaxNumber']['Value'], $record['PhysicalAddressLine1']['Value'], $record['PhysicalAddressLine2']['Value'], $record['PhysicalAddressLine3']['Value'], $record['PhysicalAddressLine4']['Value'], $record['PhysicalAddressLine5']['Value'], $record['PostalAddressLine1']['Value'], $record['PostalAddressLine2']['Value'], $record['PostalAddressLine3']['Value'], $record['PostalAddressLine4']['Value'], $record['PostalAddressLine5']['Value'], $record['BankName']['Value'], $record['BankBranchName']['Value'], $record['BankBranchCode']['Value'], $record['BankAccountNumber']['Value'], $record['ContactPersonName']['Value'], $record['ContactPersonNumber']['Value'], $record['ContactPersonEmail']['Value'], $record['AdminContactPersonName']['Value'], $record['AdminContactPersonNumber']['Value'], $record['AdminContactPersonEmail']['Value'], $record['Longitude']['Value'], $record['Latitude']['Value'], $record['BusinessEntityId']['Value']); $this::$Branch = $record; // echo $sqlCommand; die; // $stmt = sqlsrv_prepare($this->dbBaseClass->conn, $sqlCommand); // Prepares a Transact-SQL query without executing it. Implicitly binds parameters. // if (!$stmt) { // return array(printf('An error was received when the function sqlsrv_prepare was called. // The error message was: %s', dbGetErrorMsg())); // } $result = sqlsrv_query(Database::getConnection(), $sqlCommand); //sqlsrv_execute($stmt); // Executes a prepared statement. if (!$result) { return array(printf('An error was received when the function sqlsrv_execute was called. The error message was: %s', dbGetErrorMsg())); } return array(); // NB!!! // Note: empty array is converted to null by non-strict equal '==' comparison. // Use is_null() or '===' if there is possible of getting empty array. }
} $gcmRegistrationId = $_REQUEST['gcmRegistrationId']; $clanID = $_REQUEST['clanID']; $retArray = array(); // Does player exist in the table $exist = doesPlayerExist($gameName, $clanID); $playerID = getPlayerID($gameName, $clanID); if ($playerID === null) { echo json_encode([$retArray = array("responseOK" => false, "responseMessage" => "No such player")]); goto EndOfFile; } if ($exist === true) { // Update the existing record $sql = "\n UPDATE gcm_users\n SET email = '{$email}', gcm_regid = '{$gcmRegistrationId}'\n WHERE game_name = '{$gameName}'\n "; $result = $db->dbQuery($sql); if ($result == false) { echo json_encode([$retArray = array("responseOK" => false, "responseMessage" => dbGetErrorMsg())]); } else { echo json_encode(array($retArray = array("responseOK" => true, "responseMessage" => 'Record Updated Successfully'))); } } else { // Create a new record $sql = "\n INSERT INTO gcm_users (\n PlayerID, clanID, gcm_regid, game_name, email, created_at\n )\n VALUES (\n {$playerID}, {$clanID}, '{$gcmRegistrationId}', '{$gameName}', '{$email}', GETDATE()\n )\n "; $result = $db->dbQuery($sql); if ($result == false) { echo json_encode([$retArray = array("responseOK" => false, "responseMessage" => dbGetErrorMsg())]); } else { echo json_encode(array($retArray = array("responseOK" => true, "responseMessage" => 'Record Inserted Successfully'))); } } EndOfFile:
function insertRecord() { global $simContractID; global $callDurationInSeconds; global $dateTimeOfCall; global $isData; global $costOfCall; global $callDestination; global $VASName; global $VASProvider; global $billMonth; global $billYear; global $bytes; global $db; global $resultMessage; global $serviceDescription; global $categoryId; $isData = $isData == true ? 1 : 0; $callDestination = trim($callDestination); $VASName = trim($VASName); $VASProvider = trim($VASProvider); $serviceDescription = trim($serviceDescription); // echo $cellphoneNumber . ' Dest: ' . $callDestination . '<br>'; $sql = "\n\t\t\tINSERT INTO [dbo].[CallDetail]\n\t\t\t\t\t\t ([SimContractID]\n\t\t\t\t\t\t ,[DateTimeOfCall]\n\t\t\t\t\t\t ,[CallDurationInSeconds]\n\t\t\t\t\t\t ,[CallDestination]\n\t\t\t\t\t\t ,[VASName]\n\t\t\t\t\t\t ,[VASProvider]\n\t\t\t\t\t\t ,[CostOfCall]\n\t\t\t\t\t\t ,[BillYear]\n\t\t\t\t\t\t ,[BillMonth]\n\t\t\t\t\t\t ,[IsData]\n\t\t\t\t\t\t ,[Bytes]\n\t\t\t\t\t\t ,[ServiceDescription]\n\t\t\t\t\t\t ,[CategoryId])\n\t\t\t\t VALUES\n\t\t\t\t\t\t ({$simContractID}\n\t\t\t\t\t\t ,'{$dateTimeOfCall}'\n\t\t\t\t\t\t ,{$callDurationInSeconds}\n\t\t\t\t\t\t ,'{$callDestination}'\n\t\t\t\t\t\t ,'{$VASName}'\n\t\t\t\t\t\t ,'{$VASProvider}'\n\t\t\t\t\t\t ,{$costOfCall}\n\t\t\t\t\t\t ,{$billYear}\n\t\t\t\t\t\t ,{$billMonth}\n\t\t\t\t\t\t ,{$isData}\n\t\t\t\t\t\t ,{$bytes}\n\t\t\t\t\t\t ,'{$serviceDescription}'\n\t\t\t\t\t\t ,{$categoryId})\n "; try { $result = $db->dbQuery($sql); if ($result == false) { $resultMessage = 'ERROR: ' . dbGetErrorMsg(); $db->dbTransactionRollback(); return; } } catch (Exception $exc) { $resultMessage = 'EXCEPTION: ' . $exc; } }
/** * @param $record : A $Division array * * @return array: An array of errors or empty if no errors * NB!!! * Note: empty array is converted to null by non-strict equal '==' comparison. * Use is_null() or '===' if there is possible of getting empty array. */ public function insert($record) { // Populate the Division array if (!is_array($record)) { return array(printf('A Division record was expected as a parameter but a %s was received', gettype($record))); } $this::$Division = $record; $sqlCommand = sprintf("\n\t\t\t\tBEGIN\n\t INSERT INTO [dbo].[BusinessEntity] (\n\t [Name]\n\t\t\t\t\t,[BusinessEntityCode]\n\t\t\t\t\t,[BusinessEntityDescription]\n\t\t\t\t\t,[BusinessEntityParentId]\n\t\t\t\t\t,[BusinessLevelId]\n\t\t\t\t\t,[Active]\n\t\t\t\t\t,[BusinessEntityShortName] )\n\t VALUES (\n\t %s,\n\t %s,\n\t %s,\n\t %s,\n\t %s,\n\t %s,\n\t %s)\n END", $this::$Division['Name']['Value'], $this::$Division['BusinessEntityCode']['Value'], $this::$Division['BusinessEntityDescription']['Value'], $this::$Division['BusinessEntityParentId']['Value'], $this::$Division['BusinessLevelId']['Value'], $this::$Division['Active']['Value'], $this::$Division['BusinessEntityShortName']['Value']); $this->dbBaseClass->dbTransactionBegin(); // $stmt = sqlsrv_prepare($this->dbBaseClass->conn, $sqlCommand); // Prepares a Transact-SQL query without executing it. Implicitly binds parameters. // if (!$stmt) { // $msg = dbGetErrorMsg(); // return array(printf('An error was received when the function sqlsrv_prepare was called. // The error message was: %s', $msg)); // } // Insert $result = sqlsrv_query(Database::getConnection(), $sqlCommand); //sqlsrv_execute($stmt); // Executes a prepared statement. if (!$result) { $msg = dbGetErrorMsg(); return array(printf('An error was received when the function sqlsrv_execute was called. The error message was: %s', $msg)); } $this->dbBaseClass->dbTransactionCommit(); return array(); // NB!!! // Note: empty array is converted to null by non-strict equal '==' comparison. // Use is_null() or '===' if there is possible of getting empty array. }
} $division = $_GET['company']; $divisionId = $_GET['divisionId']; $displayType = $_GET['displayType']; try { $dbBaseClass = new BaseDB(); } catch (Exception $exc) { if (Database::getConnection() === false) { die("ERROR: Could not connect. " . printf('%s', dbGetErrorMsg())); } } $divisionBase = new BaseBusinessEntity(); // $companyBase = new BaseCompany(); $prep = $dbBaseClass->getFieldsForAll("BusinessEntity", array('Id', 'Name'), "WHERE BusinessEntityParentId = {$division}"); if ($displayType == 'd' || $displayType == 'r') { while ($division = sqlsrv_fetch_array($prep, SQLSRV_FETCH_ASSOC)) { if ($division['Id'] == $divisionId) { echo "<option selected = \"selected\" value=" . $division['Id'] . ">{$division['Name']}</option>"; return; } } } while ($division = sqlsrv_fetch_array($prep, SQLSRV_FETCH_ASSOC)) { if ($division['Id'] == $divisionId) { echo "<option selected = \"selected\" value=" . $division['Id'] . ">{$division['Name']}</option>"; } else { echo "<option value=" . $division['Id'] . ">{$division['Name']}</option>"; } } $err = printf('%s', dbGetErrorMsg());
echo '<h2>Using getAllByFieldname</h2>'; $stmt = $dbBaseClass->getAllByFieldName('Company', 'Name', 'Protea Security'); if ($stmt === false) { die(dbGetErrorMsg()); } while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { echo $row['Name'] . ", " . "<br />"; } sqlsrv_free_stmt($stmt); // Using getFieldsByFieldname echo '<h2>Using getFieldsByFieldname</h2>'; $fields = array('Name', 'CompanyCode'); $stmt = $dbBaseClass->getFieldsByFieldName('Company', 'Name', 'Protea Security', $fields); if ($stmt === false) { die(dbGetErrorMsg()); } while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { echo "{$row['Name']} , {$row['CompanyCode']} <br />"; } sqlsrv_free_stmt($stmt); // Using getFieldsForAll echo '<h2>Using getFieldsForAll</h2>'; $fields = array('Name', 'CompanyCode'); $stmt = $dbBaseClass->getFieldsForAll('Company', $fields); if ($stmt === false) { die(dbGetErrorMsg()); } while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { echo "{$row['Name']} , {$row['CompanyCode']} <br />"; } sqlsrv_free_stmt($stmt);
} sanitizeString($action); // Set up DB connection $dbBaseClass = new BaseDB(); $recordBase = BaseCompany::$company; if (Database::getConnection() === false) { $_SESSION['error'] = "ERROR: Could not connect. " . printf('%s', dbGetErrorMsg()); header("Location: BranchDisplayGrid.php"); exit; } // An existing record is expected when the action is not "Create" if ($action != 'c') { // Read the record $records = $dbBaseClass->getAll('Company', "WHERE id = {$id}"); if ($records === false) { $_SESSION['error'] = dbGetErrorMsg(); header("Location: BranchDisplayGrid.php"); exit; } // Get the specific record $record = sqlsrv_fetch_array($records, SQLSRV_FETCH_ASSOC); } $companyBase = BaseCompany::$company; function echoField($fieldIdName, $hidden = false) { global $action; global $record; global $recordBase; $fieldParams = initializeFieldParametersArray($fieldIdName, $recordBase); if ($action == 'r' || $action == 'd') { $fieldParams[FieldParameters::disabled_par] = 'Disabled';
if ($_POST['Active'] == 'Active') { $active = 1; } else { $active = 0; } $businessEntityBase = new BaseBusinessEntity(); $sqlCommand = "BEGIN INSERT INTO [dbo].[BusinessEntity]\n ([Name]\n ,[BusinessEntityCode]\n ,[BusinessEntityDescription]\n ,[BusinessEntityParentId]\n ,[BusinessLevelId]\n ,[Active]\n ,[BusinessEntityShortName])\n VALUES\n ( '{$_POST['Name']}'\n ,'{$_POST['BranchCode']}'\n ,''\n ,{$_POST['Division']}\n ,3\n ,{$active} ,'') END"; $result = sqlsrv_query(Database::getConnection(), $sqlCommand); if ($result) { $sqlIdentity = "select @@identity as EntityId"; $resultIdentity = sqlsrv_query(Database::getConnection(), $sqlIdentity); $rowIdentity = sqlsrv_fetch_array($resultIdentity); $entityId = $rowIdentity["EntityId"]; } else { echo printf('An error was received when the function sqlsrv_query was called. The error message was: %s', dbGetErrorMsg()); die; } // Create a Branch record (add the entity id from above $record = PopulateRecord($_POST, $recordTemplate); $record['BusinessEntityId']['Value'] = $entityId; $validateErrors = ValidateRecord($record); if ($validateErrors) { foreach ($validateErrors as $error) { echo "<div class='error'><h3>{$error}</h3></div><br>"; } die; } $updateErrors = $recordBase->insert($record); if ($updateErrors) { foreach ($updateErrors as $error) {
<?php session_start(); $clanID = $_SESSION['selectedClanID']; include_once "BaseClasses/BaseDB.class.php"; include_once "BaseClasses/Database.class.php"; $warDate = $_REQUEST['wardate']; $NumberOfParticipants = $_REQUEST['numberofparticipants']; $WarsWeWon = $_REQUEST['warswewon']; $WarsTheyWon = $_REQUEST['warstheywon']; $active = $_REQUEST['active']; $db = new BaseDB(); $sql = "insert into War(ClanId, date, NumberOfParticipants, WarsWeWon, WarsTheyWon, active) values(\n {$clanID}, '{$warDate}','{$NumberOfParticipants}', '{$WarsWeWon}', '{$WarsTheyWon}', '{$active}')"; $result = $db->dbQuery($sql); if ($result == false) { echo json_encode(['errorMsg' => 'An error occured: ' . dbGetErrorMsg()]); } else { echo json_encode(['success' => 'success']); } // if ($result == false) { // echo json_encode([ // 'errorMsg' => 'An error occured: ' . dbGetErrorMsg() // ]); // } else { // $sqlIdentity = "select @@identity as EntityId"; // $resultWarID = sqlsrv_query(Database::getInstance()->getConnection(), $sqlIdentity); // $rowIdentity = sqlsrv_fetch_array($resultWarID); // $WarID = $rowIdentity["EntityId"]; // }
$action = 'c'; $id = -1; } sanitizeString($action); // Set up DB connection if (Database::getConnection() === false) { $_SESSION['error'] = "ERROR: Could not connect. " . printf('%s', dbGetErrorMsg()); header("Location: BranchDisplayGrid.php"); exit; } // An existing record is expected when the action is not "Create" if ($action != 'c') { // Read the record $records = $dbBaseClass->getAll('Branch', "WHERE id = {$id}"); if ($records === false) { $_SESSION['error'] = "ERROR: Could not connect. " . printf('%s', dbGetErrorMsg()); header("Location: BranchDisplayGrid.php"); exit; } // Get the specific record $record = sqlsrv_fetch_array($records, SQLSRV_FETCH_ASSOC); } $recordBase = BaseBranch::$Branch; function echoField($fieldIdName, $hidden = false) { global $action; global $record; global $recordBase; $fieldParams = initializeFieldParametersArray($fieldIdName, $recordBase); if ($action == 'r' || $action == 'd') { $fieldParams[FieldParameters::disabled_par] = 'Disabled';
<?php require_once 'GCM_Loader.php'; $selectedWarID = $_REQUEST['selectedWarID']; $ownRank = $_REQUEST['rank']; $db = new BaseDB(); //TODO Remember to change the hardcoded '0' (RankByExperience) below -> BIT $sql_callFunc = "SELECT (SELECT dbo.[PlayersNextBestAttack]({$selectedWarID}, {$ownRank}, 0) AS RankToAttack) AS RankToAttack"; $result = $db->dbQuery($sql_callFunc); $rankToAttack = 0; $err = ""; $record = sqlsrv_fetch_array($result, SQLSRV_FETCH_BOTH); if ($record == false) { $err = dbGetErrorMsg(); } $db->Free($result); $db->close(); $data['rankToAttack'][0] = array('rank' => $record['RankToAttack']); echo json_encode($data);
<?php include_once "BaseClasses/BaseDB.class.php"; include_once "BaseClasses/Database.class.php"; $selectedWarID = $_REQUEST['selectedWarID']; $db = new BaseDB(); $sql = "\n UPDATE Attack SET BusyAttackingRank = 0\n WHERE ISNULL(OurParticipantID, -1) <> -1\n AND BusyAttackingRank > 0\n AND WarID = {$selectedWarID}\n AND OurAttack = 1\n "; $result = $db->dbQuery($sql); if (!$result) { $data['result'][0] = array('success' => 0, 'error' => dbGetErrorMsg()); } else { array('success' => 1, 'error' => ''); } $db->close();
<?php /** * Created by PhpStorm. * User: dutoitd1 * Date: 2014/10/22 * Time: 08:42 AM */ include "Header.inc.php"; $recordBase = new BaseBusinessEntity(); if (Database::getConnection() === false) { die("ERROR: Could not connect. " . printf('%s', dbGetErrorMsg())); } $recordTemplate = BaseBusinessEntity::$BusinessEntity; if (!isset($_POST['id']) && !isset($_POST['Create'])) { echo "<div class='error'> <h3>No ID was received for ACTION.PHP</h3></div>"; die; } if (isset($_POST['Update'])) { // $action = 'Update'; $record = PopulateRecord($_POST, $recordTemplate); $validateErrors = ValidateRecord($record); $updateErrors = $recordBase->update("id", $_POST['id'], $record); if ($validateErrors) { foreach ($validateErrors as $error) { echo "<div class='error'><h3>{$error}</h3></div><br>"; } } if ($updateErrors) { echo "<div class='error'><h3>{$updateErrors}</h3></div><br>"; }
public function update($id, $value, $changeRecord) { if (!is_array($changeRecord)) { return array(printf('A Company Record was expected as a parameter but a %s was received', gettype($changeRecord))); } //populate the company array $this::$BusinessEntity = $changeRecord; $sqlCommand = sprintf("\n\t\t\t\tBEGIN\n\t\t\t\tUPDATE BusinessEntity\n\t\t\t\tSET\n\t\t\t\t[Name] = '%s',\n\t\t\t\t[BusinessEntityCode] = '%s',\n\t\t\t\t[BusinessEntityDescription] = '%s',\n\t\t\t\t[BusinessEntityParentId] = %s,\n\t\t\t\t[BusinessLevelId] = %d,\n\t\t\t\t[Active]= %d,\n\t\t\t\t[BusinessEntityShortName] = '%s' WHERE %s = '%s' END", $this::$BusinessEntity['Name']['Value'], $this::$BusinessEntity['BusinessEntityCode']['Value'], $this::$BusinessEntity['BusinessEntityDescription']['Value'], $this::$BusinessEntity['BusinessEntityParentId']['Value'], 2, $this::$BusinessEntity['Active']['Value'], $this::$BusinessEntity['BusinessEntityShortName']['Value'], $id, $value); $stmt = sqlsrv_prepare(Database::getConnection(), $sqlCommand); // Prepares a Transact-SQL query without executing it. Implicitly binds parameters. if (!$stmt) { return array(printf('An error was received when the function sqlsrv_prepare was called. The error message was: %s', dbGetErrorMsg())); } $result = sqlsrv_execute($stmt); // Executes a prepared statement. if (!$result) { return array(printf('An error was received when the function sqlsrv_execute was called. The error message was: %s', dbGetErrorMsg())); } return array(); }