ERROR in PDO : Call to a member function prepare() on null

别等时光非礼了梦想. 提交于 2019-12-02 09:18:58

There are 2 big issues in your code:

  1. Variable visibility
  2. Static call

In detail:

  1. In oop you should forget about global variables. They are against the principle of encapsulation. Moreover, you do not even have any global variable in your code, so global $db; line is meaningless. Declare a private $db variable on class level (property) initialise it in the connection() method and access it in the insert method.

  2. You are calling the connection method as classo::connection();. However, you would need to declare connection method as static. Either declare your connection method as static (but then change $db into static as well), or call it as a regular method using $this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!