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

前端 未结 1 789
感情败类
感情败类 2021-01-27 12:40

I have a problem with prepare function ==> Call to a member function prepare() on null i have tow pages classo.php and index.php

class

相关标签:
1条回答
  • 2021-01-27 12: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.

    0 讨论(0)
提交回复
热议问题