国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Après un appel inter-classes, la méthode est introuvable
方人胥
方人胥 2021-06-12 17:16:18
0
2
1215

Exercice de surcharge de méthode statique pour obtenir un accès cha?né à la base de données

demo5.php fait référence à la classe de Query.php

Puis en accédant à la méthode dans Query, la page m'informe que la méthode 'table' n'a pas été trouvée dans la base de données

Ce qui suit Ceci est le code source, j'espère que vous pourrez m'aider à vérifier les erreurs

Le premier est Query.php

<?php
//常用的數(shù)據(jù)查詢操作
class Query
{
    //連接對象
    public $pdo = null;

    //數(shù)據(jù)表名稱
    public $table = '';

    //字段列表
    public $field = '';

    //查詢條件
    public $where = '';

    //顯示數(shù)量
    public $limit = 0;


    //構(gòu)造方法
    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    //調(diào)用表名
    public function table($tableName)
    {
        $this->table = $tableName;
        //關(guān)鍵是這一步
        return $this;
    }

    //調(diào)用字段
    public function field($fields)
    {
        $this->field = $fields;
        //關(guān)鍵是這一步
        return $this;
    }

    //設(shè)置查詢條件
    public function where($where)
    {
        $this->where = $where;
        return $this;
    }
    //設(shè)置顯示數(shù)量
    public function limit($limit)
    {
        $this->limit = $limit;
        return $this;
    }

    //創(chuàng)建SQL語句查詢
    public function select()
    {
        //設(shè)置查詢條件
        $fields = empty($this->field) ? '*' : $this->field;
        $where = empty($this->where) ? '' : ' WHERE ' . $this->where;
        $limit = empty($this->limit) ? '' : ' LIMIT ' . $this->limit;

        //SQL
        $sql = 'SELECT '.$fields. 'FROM' .$this->table. $where . $limit;

        //預(yù)處理執(zhí)行
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}

Le second est demo5.php

<?php
//方法重載實(shí)例演示
require 'Query.php';
class Database
{
    //數(shù)據(jù)庫連接對象
    protected static $pdo = null;

    //數(shù)據(jù)庫連接方法,每次查詢時(shí)再連接,實(shí)現(xiàn)真正的惰性連接,節(jié)省系統(tǒng)開銷
    public static function connection()
    {
        self::$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','admin');
    }

    //靜態(tài)方法的重載,實(shí)現(xiàn)跨類調(diào)用
    public static function __callStatic($name,$arguments)
    {
        //連接上數(shù)據(jù)庫
        self::connection();

        //實(shí)例化查詢類
        $query = new Query(self::$pdo);

        //訪問Query中的方法
        return call_user_func_array([$query, $name],[$arguments[0]]);
    }


}

$cats = Database::table('category')
    ->field('cate_id, name, alias')
    ->where('cate_id>=2')
    ->select();

foreach($cats as $cat){
    print_r($cat);
}

方人胥
方人胥

répondre à tous(2)
方人胥

J'ai trouvé le problème, c'est la ligne 65 de Query.php Lors de l'épissage, des espaces doivent être ajoutés avant et après FROM.

方人胥

La méthode d'invite de page 'table' n'a pas été trouvée dans la base de données

image.png

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal