Error syntax error, unexpected '[' in

0

Good afternoon guys, I'm having a problem with my DB, but I do not know how to solve it, the error is this:

Parse error: syntax error, unexpected '[' in /home/storage/2/3e/eb/noticiasdasgerais/public_html/yteste/Lib/DB.php on line 44

The line of code 44 is:

public function fetchAll($sql) {

        if ($this->pagination == true) {
            $res = $this->con->query($sql) or die($this->con->errorInfo()[2]); //tratamento de exceções  Linha 44
            $this->rows = count($res->fetchAll(PDO::FETCH_OBJ));
            $this->paginateLink();
            $sql .= " LIMIT $this->page, $this->perpage";
            $this->pagination = false;
        }
        $res = $this->con->query($sql) or die($this->con->errorInfo()[2]); //tratamento de exceções
        if ($res) {
            $this->data = $res->fetchAll(PDO::FETCH_OBJ);
            return $this->data;
        }
    }

Alias the php version of my hosting is 5.2

    
asked by anonymous 23.05.2017 / 16:48

1 answer

2

It's the same problem with this other question you asked yourself:

Error syntax error, unexpected T_OBJECT_OPERATOR

This system you are trying to use was probably written by someone who was not careful to maintain backward compatibility where he uses new features from newer versions of PHP.

This feature was added in PHP5.4: link

  

Function array dereferencing has been added, e.g. foo () [0].

In version 5.2 it is not possible to do this type of access:

$this->con->errorInfo()[2]

Assign the method return to a variable and then access the array index.

$error = $this->con->errorInfo();
echo $error[2];

Example for your case:

$res = $this->con->query($sql) or die($this->con->errorInfo()[2]); 

Correct like this:

if (!($res = $this->con->query($sql))) {
    $error = $this->con->errorInfo();
    die($error[2]);
}
    
23.05.2017 / 17:02