0

I am working on a php method that giving me a syntax error on the third line of the method body. Commenting out the line does not do anything for me. Only by commenting out the entire method, does it actually go away. Am I missing something here?

public function get(Array $array) {
    if(array_key_exists('data', $array)) {
        if($array['data'] == '*') {
            $fieldString = '*';
        } else { 
            $fieldString = '';
            for($x=0; $x < count($array['data']); $x++) {
                if($x == count($array['data']) - 1 ) {
                    $fieldString .= $array['data'][$x].' ';
                } else {
                    $fieldString .= $array['data'][$x].', ';
                }
            }
        }
        if(array_key_exists('table', $array)) {
            $table = $array['table'];
        }
        if(array_key_exists('conditions', $array)) {
            $condition = $array['conditions'];
            $filter = '';
            foreach($condition['cond'] as $cond) {
                if(array_key_exists('type', $cond)) { 
                    $filter .= $cond['type'].' ';
                }
                if(array_key_exists('field', $cond)) {
                    $filter .= $cond['field']. ' = '; 
                }
                if(array_key_exists('value', $cond)) {
                    $filter .= $cond['value'];
                }
            }
        }

        $result = $mysqli->query("SELECT ".$fieldString." FROM ".$table. " ".$filter);
        $response = $result->fetch_assoc();
        if(!empty($response)) {
            return $response;
        } else {
            echo 'response array from get model is empty';
        }
    } else {
        echo '<h3>array data not set for _get() in model </h3>';
    }    
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
appthat
  • 816
  • 2
  • 10
  • 30

2 Answers2

1

Is the method contained within the class. Sometimes I accidentally put a class function at the end of the class file but outside the ending }. If it is not a class method, remove 'public'.

mseifert
  • 5,390
  • 9
  • 38
  • 100
  • I get a `parse error` when I try to define this method outside a class, not `unexpected T_STRING`. – Barmar Dec 06 '13 at 17:25
  • Good catch. I saw (T_PUBLIC) and made the connection. – mseifert Dec 06 '13 at 17:30
  • I put this method inside a class Model { function get(){ ... } }. I did check to make sure that class braces closed around it and all other code in this class is commented out to eliminate other factors – appthat Dec 06 '13 at 17:40
0

Change $mysqli->query() to $this->mysqli->query()

I am presuming you created a class method mysqli but you are calling it without '$this'. If mysqli is the php function, it should be mysqli_query($this->token, "query")

Although, you should be getting ' Undefined variable' with this. In any event $mysqli is undefined in this method.

mseifert
  • 5,390
  • 9
  • 38
  • 100