dongpan5289
2017-06-10 19:35
浏览 46
已采纳

致命错误:调用未定义的方法connectDB :: prepare()

I tried to do a simple CRUD using a class connectDB to connect to a MySQL database. Then when I try to execute the method delRecipe of the class RecipesModel the system show me an error.

Fatal error: Call to undefined method connectDB::prepare()

Is the right way to call the method prepare()? Why is not recognized?

Here is the code of connectDB (file connectDB.php)

class connectDB {

    private $address='localhost';
    private $db_name='db-name';
    private $user='root';
    private $pswd='psswd';

    private $sql;

    public function __construct() {
        $this->sql = new mysqli($this->address,$this->user,$this->pswd,$this->db_name);
        if (mysqli_connect_error()) {
            die('Error de Conexion: '. mysqli_connect_errno().' - '.mysqli_connect_error());
        }
        return $this->sql;
    }

    public function __destruct() {
        if(!mysqli_close($this->sql)) {
            die('ERROR!:'.$this->sql->error);
        }
    }

    public function execute($query) {
        $res = $this->sql->query($query);
        if ($res) {
            return $res;    
        }
        else {
            die('ERROR!:'.$this->sql->error);
        }
    }
}

And the class that delete the row.

<?php
require_once('connectDB.php');

class RecipesModel {

    private $db;

    public function __construct() {
        $this->db = new connectDB();
    }

    public function delRecipe($id) {
        if (is_numeric($id)) {
            $sql = 'DELETE FROM t_platos WHERE ID_pl= ?';
            $this->db->prepare($sql);
            return $this->db->execute(array($id));
        }
    }
}

$recipe = new RecipesModel();
$res = $recipe->delRecipe(1);

?>

图片转代码服务由CSDN问答提供 功能建议

我尝试使用connectDB类连接到MySQL数据库进行简单的CRUD。 然后,当我尝试执行RecipesModel类的方法delRecipe时,系统显示错误。

致命错误:调用未定义的方法connectDB :: prepare()< / p>

调用方法prepare()的正确方法是什么? 为什么不被识别?

这是connectDB的代码(文件connectDB.php)

  class connectDB {
 
 private  $ address ='localhost'; 
 private $ db_name ='db-name'; 
 private $ user ='root'; 
 private $ pswd ='psswd'; 
 
 private $ sql; 
 \  n public function __construct(){
 $ this-&gt; sql = new mysqli($ this-&gt; address,$ this-&gt; user,$ this-&gt; pswd,$ this-&gt; db_name); 
  if(mysqli_connect_error()){
 die('Error de Conexion:'。mysqli_connect_errno()。' - '。mysqli_connect_error()); 
} 
返回$ this-&gt; sql; 
} 
 \  n public function __destruct(){
 if(!mysqli_close($ this-&gt; sql)){
 die('ERROR!:'。$ this-&gt; sql-&gt; error); 
} 
  } 
 
公共函数执行($ query){
 $ res = $ this-&gt; sql-&gt; query($ query); 
 if($ res){
 return $ res;  
} 
其他{
 die('错误!:'。$ this-&gt; sql-&gt;错误); 
} 
} 
} 
   
  
 

删除该行的类。

 &lt;?php 
require_once('connectDB.php'); 
 
class RecipesModel {
 
  n private $ db; 
 
 public function __construct(){
 $ this-&gt; db = new connectDB(); 
} 
 
 public function delRecipe($ id){
 if(is_numeric(  $ id)){
 $ sql ='DELETE FROM t_platos WHERE ID_pl =?'; 
 $ this-&gt; db-&gt; prepare($ sql); 
 return $ this-&gt; db-&gt; execute  (array($ id)); 
} 
} 
} 
 
 $ recipe = new RecipesModel(); 
 $ res = $ recipe-&gt; delRecipe(1); 
 
?&gt;  ; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutian4046 2017-06-10 19:42
    已采纳

    Your connectDB class does not have a method called prepare().

    You're probably trying to call $this->db->sql->prepare(), since in connectDB you store the actual database connection into $this->sql. However, since the $sql property is private, you can't do that.

    You'll either need to make $sql a public property, or create a method in your connectDB class to act as a proxy.

    点赞 打赏 评论
  • douweilei2307 2017-06-10 19:55

    Your misunderstanding is this:

        return $this->sql;
    

    You can't return something of your choice in a constructor. The value returned by a constructor is always the object instance of that class.

    点赞 打赏 评论

相关推荐 更多相似问题