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

时间:2014-10-30 10:57:57

标签: php pdo

我试图创建一个函数,无论何时单击该按钮,该函数都会在数据库中插入一些内容。我正在创建使用PDO,但它似乎没有工作,它给我一个错误。如果有人可以帮忙请指出我正确的方向。 Fatal error: Call to undefined method DBEngine::prepare() line 87

DB_Config:

<?php
    class   DBEngine
        {
            public  $con;
            public  $errors;
            public  function __construct($host="mysql10.000webhost.com",$db = "a6150953_test",$user="a6150953_henri",$pass="hajvan123")
                {
                    try {
                            $this->con  =   new PDO("mysql:host=$host;dbname=$db",$user,$pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
                        }
                    catch (Exception $e) {
                          $this->errors['connect']['message']    =   $e->getMessage();
                          $this->errors['connect']['error_code'] =   $e->getCode();
                        }
                }

                public  function Fetch($_sql)
                {
                    $query  =   $this->con->prepare($_sql);
                    $query->execute();
                    $this->errors['fetch'][]    =   $query->errorInfo();

                    if($query->rowCount() > 0) {
                            while($rows = $query->fetch(PDO::FETCH_ASSOC)) {
                                    $array[]    =   $rows;
                                }
                        }

                    return (isset($array) && $array !== 0 && !empty($array))? $array: 0;
                }

            // Simple write to db method
            public  function Write($_sql)
                {
                    $query  =   $this->con->prepare($_sql);
                    $query->execute();
                    $this->errors['insert'][]   =   $query->errorInfo();
                }
        }

// To use/initialize
$con = new DBEngine(); ?>

动作

<?php
session_start();
include_once('php/classes/db_config.php');
include_once('php/classes/class.user.php');

$follow = new User($con);

echo $is_uid = $_SESSION['uid'];
echo $action = $_GET['action'];
echo $def_uid = $_GET['u_id'];

if($action == "follow"){
    $follow -> insert($is_uid, $def_uid);
}
if($action == "unfollow"){
    $follow -> delete($is_uid, $def_uid);
}

?>

函数(仅包含两个函数而不是整个页面)

public function insert($is_uid, $def_uid){
        $sql        =   "INSERT INTO follow (user_one, user_two) VALUES (:user_one, :user_two)";
        $q = $this->db->prepare($sql);
        $q -> execute(array(':user_one'=>$is_uid, ':user_two'=>$def_uid));

        return $q;
    }
    public function delete($is_uid, $def_uid){
        $sql        =   "DELETE FROM follow WHERE user_one = '$is_uid' AND user_two = '$def_uid'";
        $result     =   $this->db->Fetch($sql);

        return $result;
    }

1 个答案:

答案 0 :(得分:2)

prepare是一种PDO方法,而不是DBEngine类中的方法。您可以像这样定义方法:

public function prepare($query) {
    return $this->con->prepare($query);
}