Cannot use Object of type Pdostatement as array

Asked

Viewed 1,495 times

1

I can’t seem to do the header location redirect to a table field.

In this field, you have a PDF document link.

Look at the code:

case 'mostra-id':  

$link = Connection::select("SELECT link FROM `arquivos` where id =" . App::$key);

$sql = "update `arquivos` set `acessos`= acessos + 1 where id=" . App::$key;
$dados = Connection::exec($sql);

header('Location: ' . $link['link']);
break;

The link in the table is saved this way: sistema/public/arquivos/pdf/01.pdf

The update is usually correct, but does not open the link when redirecting. Giving the following error:

Fatal error: Cannot use Object of type Pdostatement as array in

  • I was half in doubt, the link only appears by header Location, but when you put the direct link by the browser it works, correct?

  • Do print_r($link); and put the result here with.

  • Pdostatement Object ( [queryString] => SELECT link FROM arquivos Where id =10 )

  • You don’t have to execute $link to get the BD result?

  • Yes, it worked like this: header('Location: ' . $link->fetchColumn(0));

2 answers

1

Cannot use Object of type Pdostatement as array in

The error suggests that the return of select is not an array but probably an object, so change the notation of:

header('Location: ' . $link['link']);

To:

header('Location: ' . $link->link);
  • Made the same mistake.

0


$link is not an array to be handled the way you did, it is an object Pdostatement then you have to handle it differently.

Try $link->fetchColumn(0), this is his documentation, this command will fetch the first column of the next result set row.

  • 1

    Worked right, man. Thank you very much.

Browser other questions tagged

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