简体   繁体   English

致命错误:未捕获错误:调用 C:\\xampp\\htdocs\\phpmvc\\app\\models\\Mahasiswa_model.php:31 中未定义的函数 query()

[英]Fatal error: Uncaught Error: Call to undefined function query() in C:\xampp\htdocs\phpmvc\app\models\Mahasiswa_model.php:31

I'm trying to make an mvc framework like ci, but I have a problem with this error.我正在尝试制作一个像 ci 这样的 mvc 框架,但是我遇到了这个错误的问题。 the error "Call to undefined function query()" but it's weird since query() works fine in method getMahasiswaById() .错误“调用未定义的函数 query()”,但这很奇怪,因为query()在方法getMahasiswaById()工作正常。

Fatal error: Uncaught Error: Call to undefined function query() in C:\\xampp\\htdocs\\phpmvc\\app\\models\\Mahasiswa_model.php:31 Stack trace: #0 C:\\xampp\\htdocs\\phpmvc\\app\\controllers\\Mahasiswa.php(27): Mahasiswa_model->tambahMahasiswa(Array) #1 C:\\xampp\\htdocs\\phpmvc\\app\\core\\App.php(35): Mahasiswa->tambah() #2 C:\\xampp\\htdocs\\phpmvc\\public\\index.php(5): App->__construct() #3 {main} thrown in C:\\xampp\\htdocs\\phpmvc\\app\\models\\Mahasiswa_model.php on line 31

This my model:这是我的模型:

class Mahasiswa_model {

    private $table = 'mahasiswa';
    private $db;

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

    public function getMahasiswa()
    {
        $this->db->query('SELECT * FROM '.$this->table);
        return $this->db->resultSet();
    }

    public function getMahasiswaById($id)
    {
        $this->db->query('SELECT * FROM '.$this->table.' WHERE id=:id');
        $this->db->bind('id', $id);
        return $this->db->single();
    }

    public function tambahMahasiswa($data)
    {
        $query = "INSERT INTO mahasiswa 
                    VALUES ('', :nama, :nim, :email, :jurusan)";

        $this->db-query($query);
        $this->db->bind('nama', $data['nama']);
        $this->db->bind('nim', $data['nim']);
        $this->db->bind('email', $data['email']);
        $this->db->bind('jurusan', $data['jurusan']);

        $this->db->execute();

        return $this->db->rowCount();
    }
}

This my controller:这是我的控制器:

<?php

class Mahasiswa extends Controller {

    public function index()
    {
        $data['title'] = 'List Mahasiswa';

        $data['mhs'] = $this->model('Mahasiswa_model')->getMahasiswa();
        $this->view('templates/header', $data);
        $this->view('mahasiswa/index', $data);
        $this->view('templates/footer');
    }

    public function detail($id)
    {
        $data['title'] = 'Detail Mahasiswa';

        $data['mhs'] = $this->model('Mahasiswa_model')->getMahasiswaById($id);
        $this->view('templates/header', $data);
        $this->view('mahasiswa/detail', $data);
        $this->view('templates/footer');
    }

    public function tambah()
    {
        if ($this->model('Mahasiswa_model')->tambahMahasiswa($_POST) > 0) {
            header ('Location: '. BASEURL .'/mahasiswa');
        }
    }
}

This my core Database.php这是我的核心 Database.php

<?php

class Database {

    private $host = DB_HOST;
    private $user = DB_USERNAME;
    private $pass = DB_PASSWORD;
    private $db_name = DB_NAME;

    private $dbh;
    private $stmt;

    public function __construct()
    {
        //data source name
        $dsn = 'mysql:host='.$this->host.';dbname='.$this->db_name;

        $option = [
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ];

        try {
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $option);
        } catch(PDOExeption $e) {
            die($e->getMessage());
        }
    }

    public function query($query)
    {
        $this->stmt = $this->dbh->prepare($query);
    }

    public function bind($param, $value, $type = null)
    {
        if(is_null($type)) {
            switch(true) {
                case is_int($value) :
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value) :
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value) :
                    $type = PDO::PARAM_NULL;
                    break;
                default :
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }

    public function execute()
    {
        $this->stmt->execute();
    }

    public function resultSet()
    {
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function single()
    {
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function rowCount()
    {
        return $this->stmt->rowCount();
    }
}

Simple syntax error:简单的语法错误:

$this->db-query($query);

See what you are missing there?看看你在那里缺少什么?

$this->db->query($query);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\shop\index.php - Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\shop\index.php 致命错误:未捕获错误:调用 C:\\xampp\\htdocs\\ 中未定义的函数 mysql_connect() - Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\ 致命错误:未捕获错误:在C:\\ xampp \\ htdocs \\ oophp \\ Home.php中调用成员函数connect()时为null - Fatal error: Uncaught Error: Call to a member function connect() on null in C:\xampp\htdocs\oophp\Home.php 致命错误:在第101行的C:\\ xampp \\ htdocs \\ Employees.php中调用未定义的函数getRecords() - Fatal error: Call to undefined function getRecords() in C:\xampp\htdocs\Employees.php on line 101 致命错误:在第53行的C:\\ xampp \\ htdocs \\ webdev \\ div.php中调用未定义的功能countdown() - Fatal error: Call to undefined function countdown() in C:\xampp\htdocs\webdev\div.php on line 53 致命错误:未捕获的错误:C:\xampp\htdocs\imagebob\google-drive.php:41 中未定义的常量“STDIN” - Fatal error: Uncaught Error: Undefined constant “STDIN” in C:\xampp\htdocs\imagebob\google-drive.php:41 致命错误:未捕获的错误:调用成员函数 prepare() 在 null [C:\\xampp\\htdocs\\twitter\\core\\classes\\user.php on line 19] - Fatal error: Uncaught Error: Call to a member function prepare() on null [C:\xampp\htdocs\twitter\core\classes\user.php on line 19] 致命错误:未捕获错误:调用 function sqlsrv_connect() in C:\xampp\htdocs\DBtest\sqlsrv.php:7 堆栈跟踪:#0 {main} SQLSERVER - Fatal error: Uncaught Error: Call to function sqlsrv_connect() in C:\xampp\htdocs\DBtest\sqlsrv.php:7 Stack trace: #0 {main} SQLSERVER 致命错误:在第71行的C:\\ xampp81 \\ htdocs \\ aaainc \\ db.php中调用未定义函数oci_connect() - Fatal error: Call to undefined function oci_connect() in C:\xampp81\htdocs\aaainc\db.php on line 71 致命错误:在第75行的C:\\ xampp81 \\ htdocs \\ test \\ inc \\ db.php中调用未定义函数oci_connect() - Fatal error: Call to undefined function oci_connect() in C:\xampp81\htdocs\test\inc\db.php on line 75
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM