在OOP PHP中重构代码,并使用PDO进行mysql查询。

时间:2021-11-03 15:15:03

I am new to OOP in PHP. I am having a problem while I am trying to refactor my code in OOP and PDO. The problem occurred when I tried refactor my code. So I have a category class to manage category CRUD operations. To read all the previously created categories I have a method

在PHP中,我是OOP的新手。当我试图在OOP和PDO中重构代码时,我遇到了一个问题。当我尝试重构我的代码时,问题就出现了。我有一个category类来管理category CRUD操作。要读取之前创建的所有类别,我有一个方法

    public function readAll(){
        $query = "SELECT * FROM " . $this->table_name . " ORDER  BY modified ASC";
       $stmt = $this->connection->prepare($query);
         if($stmt->execute()){
            return $stmt;
          } 
  }

And within the same class in the construct function I am getting my db connection like so

在构造函数的同一个类中,我得到了这样的db连接

public function __construct(PDO $db){
    $this->connection = $db;
} 

and I am calling the read all method in my page like this -

我在我的页面中调用read all方法,就像这样

 $allCategories= $category->readAll();

and it works just fine but when I tried to refactor my code to use a new function like this-

它工作得很好,但是当我试图重构我的代码来使用一个像这样的新函数时

    private function doQuery($query){
        $stmt = $this->connection->prepare($query);
         if($stmt->execute()){
            return $stmt;
          }    
    }
    public function readAll(){
        $query = "SELECT * FROM " . $this->table_name . " ORDER  BY modified ASC";
      $this->doQuery($query);
  }

I am getting the following error :

我得到以下错误:

Fatal error: Call to a member function fetch() on a non-object in D:\xampp\htdocs\image-upload\category.php on line 222

So I just want to save myself from having to write prepare and execute statements every time I make a different query. I am trying to make my code DRY. I may be missing something. A slight hint in the right direction is highly appreciated. Please help me.

我只是想避免每次做不同的查询时都要编写准备和执行语句。我试着让我的代码变干。我可能漏掉了什么。在正确的方向上有一点小小的暗示是非常值得赞赏的。请帮助我。

" THANKS IN ADVANCE "

"提前谢谢"

1 个解决方案

#1


3  

your second readAll() method returns nothing. It should read

第二个readAll()方法不返回任何内容。它应该阅读

return $this->doQuery($query);

or, rather, based on the method's name

或者,更确切地说,基于方法的名称

return $this->doQuery($query)->fetchAll();

to get an array with all the rows selected.

获取一个数组,其中包含选定的所有行。

Aside from this trifle issue, there is a mistake in the doQuery() method, making it utterly useless. It have to be

除了这个小问题之外,doQuery()方法中还有一个错误,使得它完全无用。它必须是

private function doQuery($query, $data = array()){
    $stmt = $this->connection->prepare($query);
    $stmt->execute($data);
    return $stmt;
}

allowing you to use placeholders to represent actual data variables in the query

允许您使用占位符来表示查询中的实际数据变量。

#1


3  

your second readAll() method returns nothing. It should read

第二个readAll()方法不返回任何内容。它应该阅读

return $this->doQuery($query);

or, rather, based on the method's name

或者,更确切地说,基于方法的名称

return $this->doQuery($query)->fetchAll();

to get an array with all the rows selected.

获取一个数组,其中包含选定的所有行。

Aside from this trifle issue, there is a mistake in the doQuery() method, making it utterly useless. It have to be

除了这个小问题之外,doQuery()方法中还有一个错误,使得它完全无用。它必须是

private function doQuery($query, $data = array()){
    $stmt = $this->connection->prepare($query);
    $stmt->execute($data);
    return $stmt;
}

allowing you to use placeholders to represent actual data variables in the query

允许您使用占位符来表示查询中的实际数据变量。