I decided to write a forum using PDO .

I do not understand what the problem is, it gives an error 500.

In a separate file, everything works fine, in the main file there.

  • File request.php:

    i.stack.imgur.com/BEdWW.png

  • File database.class.php:

enter image description here

  • File function.class.php:

enter image description here

Closed due to the fact that off-topic participants zRrr , D-side , Vartlok , user194374, aleksandr barakin 31 Mar '16 at 7:14 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - zRrr, D-side, Vartlok, Community spirit, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • No .. I can not understand what the problem is for several hours exactly - DimaRRR
  • one
    And you watched the web server logs? with 500 errors, their essence usually remains in error_log ... - Mike
  • Error: PHP Fatal error: Call to a member function query () on a non-object in / tmp/m_forum/function.class.php on line 28 foreach ($ pdo-> query ("SELECT * FROM umbrella_forum ") as $ row) - DimaRRR
  • 2
    @DimaRRR Paste the code in the body of the question as text, not an image. - Nicolas Chabanovsky
  • ... and the error message, too, is part of the question after all. The error usually contains half the solution. - D-side

1 answer 1

In the getForums() function that is represented in the image, the $pdo variable is not declared. Apparently this is an object for working with the database, which is declared in the global scope. But in php in the body of the function global variables are not available until they are explicitly declared:

 $wow='wow'; //глобальная переменая function sayWow(){ global $wow; //теперь переменная доступна в теле функции echo $wow; } 

Variable scope

In your case, most likely, getForums() is a method of some class. So use $this :

 public function getForums(){ /* ... */ $this->pdo->query('SELECT * FROM forums'); /* ... */ } 

And don't forget to inject $pdo in a constructor or a separate method.

Classes and objects