I am attempting to put together a database query construction class that will construct strings for prepared statements.
function SetOperVals($cols,$oper) {
foreach(array_combine($cols,$oper) as $c => $o) {
$cv[$i] = $c.$o.'?';
$i++;
}
$this->OperVals = $cv;
}
I can var_dump($cv) from within this function and I see the output I'm expecting, but when I use the below method to get the value it returns null:
function GetOperVals() {
return $this->OperVals;
}
I'm positive it's something really obvious because it's late and I'm tired.... .... ... any help is appreciated - thanks in advance!
// As requested - the full class below along with how it's being called:
class sql {
// need some conditions in here to determine how to setup the string
function ConstructPrepared() {
return $this->GetQueryType().' '
. $this->GetColumns().' '
. $this->GetAction().' '
. $this->GetSchema().''
. $this->GetTable().' '
. $this->GetConditions()
;
}
function GetConditions() {
return $this->Conditions;
}
function GetConditionsAndValues(){
return $this->ConditionsAndValues;
}
function GetOperVals() {
return $this->OperVals;
}
function GetQueryType() {
return $this->Type;
}
function GetAction(){
return $this->Action;
}
function GetSchema(){
return $this->Schema;
}
function GetTable(){
return $this->Table;
}
function GetColumns(){
return $this->Cols;
}
function GetVals(){
return $this->Vals;
}
function GetColsandVals(){
return $this->ColsVals;
}
function WrapString($s) {
if( gettype($s) == string) {
return '\''.$s.'\'';
} else { return $s; }
}
function SetOperVals($cols,$oper) {
$i = 0;
foreach(array_combine($cols,$oper) as $c => $o) {
$cv[$i] = $c.$o.'?';
$i++;
}
$this->OperVals = $cv;
}
function SetQueryType($type) {
$this->SetAction($type);
switch(strtolower($type)) {
case 's' : case 'select' : $this->Type = 'select'; break;
case 'i' : case 'insert' : $this->Type = 'insert'; break;
case 'u' : case 'update' : $this->Type = 'update'; break;
case 'd' : case 'delete' : $this->Type = 'delete'; break;
}
}
function SetAction($action) {
switch(strtolower($action)) {
case 's' : case 'select' : $this->Action =' from '; break;
case 'i' : case 'insert' : $this->Action = ' into '; break;
case 'u' : case 'update' : $this->Action = ''; break;
case 'd' : case 'delete' : $this->Action = ' from '; break;
}
}
function SetTable($table) {
$this->Table = $table;
}
function SetSchema($schema) {
if($schema) {
$this->Schema = $schema.'.';
} else $this->Schema = null;
}
function SetColumns($columns, $oper) {
switch($this->GetQueryType()) {
case 'select' : $this->Cols = implode(',',$columns); break;
case 'insert' : $this->Cols = implode(',',$columns); break;
case 'delete' : $this->Cols = null; break;
case 'update' : $this->Cols = SetColsandVals($columns , GetVals(), $oper);
}
}
function SetVals($values) {
switch(GetType()) {
case 's' : $this->Vals = implode(',',$values); break;
case 'i' : $this->Vals = implode(',',$values); break;
case 'd' : $this->Vals = null; break;
case 'u' : $this->Vals = $values; // pass them on to the ColsandVals function in the SetColumns function, do nothing here
}
}
function SetColsandVals($cols,$oper) {
$this->ColsVals = GetOperVals($cols,$oper);
}
function SetConditionsAndValues($cols,$oper) {
// var_dump($cols,$oper);
$this->ConditionsAndValues = $this->GetOperVals($cols,$oper);
}
function SetConditions($cols,$vals,$oper) {
var_dump($cols,$oper);
if($cols) {
$this->Conditions = 'where '.$this->GetConditionsAndValues($cols,$oper);
}
}
}
And here is how it's called in the index.php test file:
$q = new sql;
$q->SetQueryType('s');
$q->SetTable('test_table');
$q->SetColumns(array('id','name'), $oper);
$q->SetConditionsAndValues(
$cols = array('ID')
, $vals = array(1)
, $ops = array('=')
);
$q->SetOperVals($cols,$ops);
echo $q->ConstructPrepared();