Syntax error error, Unexpected '[' in

Asked

Viewed 567 times

0

Good afternoon guys, I have a problem with my DB, but I can’t solve, 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 in 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

1 answer

2


It’s the same problem with that other question you asked yourself:

Syntax error, Unexpected T_OBJECT_OPERATOR

This system you are trying to use was probably written by someone who was not careful to keep a backward compatibility in the passages where it uses new features of newer versions of PHP.

This feature was added in PHP5.4: http://docs.php.net/manual/en/migration54.new-features.php

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 return of the method to a variable to then access the index of the array.

$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]);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.