douzhi7451 2016-08-27 06:13
浏览 57

数组值未传递到set方法值

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();
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度