Firstly Apologies, this is a pain in the ass to decribe and plus it's my first OOP app. so if I miss some fundamental mistake I'm sorry.
I'll give you the basic info and then I'll describe my problem.
Database:
user_table
userID -> int
userFname -> nvarhcar
userLname -> nvarhcar
userAddress -> int (relation to adderID in addr_table)
userAge -> int
addr_table
addrID -> int
addrStreetNum -> int
addrStreet -> nvarchar
addrSuburb -> nvarchar
addrState -> nvarchar
addrCountry -> nvarchar
addrPCode -> nvarchar
PHP Classes
class ConnectionDB {
public $storedProcedures = array();
private $db_server;
private $db_user;
private $db_password;
private $db;
function __construct() {
$db_server = "xxxxxx";
$db_user = "xxxxxx";
$db_password = "xxxxxx";
$db = "xxxxxx";
$db_connection = mssql_connect($db_server, $db_user, $db_password);
$db_select = mssql_select_db($db);
include_once("project.dbSettings");
$this->storedProcedures = $storedProcedures;
}
function buildSQL ($tSP, $tData) {
$myEval = '$query = mssql_init("'.$this->storedProcedures[$tSP]['name'].'");' . "\r\n";
$tData = array_values($tData);
$i = 0;
foreach($this->storedProcedures[$tSP]['arguments'] as $item) {
$myEval .= '$str_'.$i.' = "' . $tData[$i]['Value'] . '";' . "\r\n";
$myEval .= 'mssql_bind($query, "@' . $item . '", &$str_' . $i . ', ' . $this->getDataType($tData[$i]['Name']) . ');' . "\r\n";
$i ++;
}
return $myEval;
}
function execSQL ($tSQL) {
$this->db_connection;
$this->db_select;
eval($tSQL);
$result = mssql_execute($query) or die($tSQL);
$num_fields = mssql_num_fields($result);
$x = 0;
while( $row = mssql_fetch_array($result) ) {
for( $j=0; $j<$num_fields; $j++ ) {
$name = mssql_field_name($result, $j);
$object[$x][$name] = $row[$name];
}
$x ++;
}
if (!isset($object)) {
$object = Array( "FALSE" );
}
mssql_close();
return $object;
}
}
An example of the project.dbSettings file:
$storedProcedures = array (
"skdcinonqmndc" => array ("name" => "test_connection",
"description" => "tests the connection to the database", "arguments" => array())
)
System class
class System {
function __construct () {}
function returnUserDetails($userID) {
$myConnection = new ConnectionDB;
$myData = array();
$myData['UID']['Value'] = $PocID;
$myData['UID']['Name'] = "txt-01";
$sp = "04jfk2jsd92";
$mySQL = $myConnection->buildSQL($sp, $myData);
$myResult = $myConnection->execSQL($mySQL);
return ($myResult);
}
function returnAddrDetails($addrID) {
$myConnection = new ConnectionDB;
$myData = array();
$myData['AddrID']['Value'] = $addrID;
$myData['AddrID']['Name'] = "txt-01";
$sp = "94f92jek2";
$mySQL = $myConnection->buildSQL($sp, $myData);
$myResult = $myConnection->execSQL($mySQL);
return ($myResult);
}
User Class:
include("System.class.php");
include("Address.class.php");
class User {
public $Fname;
public $Lname;
public $objAddr;
public $Age;
public $id;
function __construct($uid) {
$this->id = $uid;
$mySystem = new System;
$arr_UserDetails = $mySystem->returnUserDetails($this->id);
$this->Fname = $arr_UserDetails[0]['userFname'];
$this->Lname = $arr_UserDetails[0]['userLName'];
$this->objAddr = new Address($arr_UserDetails[0]['userAddress ']);
$this->Age = $arr_UserDetails[0]['userAge'];
}
}
Address Class:
include_once("System.class.php");
class Address {
public $StreetNum;
public $Street;
public $Suburb;
public $State;
public $Country;
public $PCode;
function __construct ($addrID) {
$this->id = $addrID;
$mySystem = new System;
$arr_AddrDetails = $mySystem->returnAddrDetails($this->id);
$this->StreetNum = $arr_AddrDetails[0]['addrStreetNum'];
$this->Street = $arr_AddrDetails[0]['addrStreet'];
$this->Suburb = $arr_AddrDetails[0]['addrSuburb'];
$this->State = $arr_AddrDetails[0]['addrState'];
$this->Country = $arr_AddrDetails[0]['addrCountry'];
$this->PCode = $arr_AddrDetails[0]['addrPCode'];
}
}
This idea behind this is that I'm hoping to be able access a users address information by doing the following:
$user = new User("1");
$country = $user->objAddr->country;
or at least something like that.
My problem though with the $this->storedProcedures variable in the Connection.class.php file.
With the first instance of the Connection class it's value is the entire contents of the $storedProcedures array in project.dbSettings, on the second subsequent
instance it loses the entire content of the array, it's type is still type array, but it is empty. Very very strange.
I know it's a headache reading through all this, if I'm doing something silly and newbie esque I am sorry, and I appreciate any help I can get.
Thanks.
S