关于 PHP 和 MVC 的初学者问题

发布于 2024-09-16 07:06:09 字数 1869 浏览 2 评论 0原文

我正在尝试学习 PHP 的 MVC 架构。所以我玩一些简单的类和函数。我找不到这段代码有什么问题,它返回:

致命错误:在非对象上调用成员函数 fetch() /opt/lampp/htdocs/test/MVC/Vue.php 上 第 15 行

这是我的代码:

Model.php:

class News {
    public function ConnBdd() {
        $this->bdd = new PDO('mysql:host=localhost;dbname=db301591273', 'root', '');
        $this->query = "SELECT Nom,IdTest,Image,DATE_FORMAT(DateCreation, '%Y-%m-%d') AS DateCreation FROM Questionnaires WHERE autorise='1' ORDER BY DateCreation DESC LIMIT 0, 4";
        $this->preparedQuery = $this->bdd->prepare($this->query);
        $this->executedQuery = $this->preparedQuery->execute();
    }
 }

Controller.php

class Controller {
    public $Model;
    public $View;
    public $News;
    public function ShowNews(){
        $this->News->ConnBdd();
        $this->View->ShowDaNews();
    }
}

View.php

class View {
    public $Model;
    public $News;

    public function ShowDaNews() {
        while ($c = $this->News->executedQuery->fetch()) {?>
    <tr>
    <td class="tableImg"><?echo '<img src="/img/ico/'.$tests['Image'].'.png" />'?></td>
    <td class="tableTest"><?echo '<a href="/page/php?t='.$tests['IdTest'].'">'.$tests['Nom'].'</a>'?></td>
    </tr>
            <?}
       }
}

和 Index.php

require_once 'Modele.php';
require_once 'Controleur.php';
require_once 'Vue.php';

$Model= new Model();
$Controller = new Controller();
$View = new View();
$Controller->Model = $Model;
$Controller->View = $View;
$News = new News();
$Controller->News = $News;
$View->News = $News;
$Controller->ShowNews();

感谢您的帮助。

I'm trying to learn MVC architecture for PHP. So I play with some simple classes and functions. I can't find what's wrong with this code, which returns a:

Fatal error: Call to a member function fetch() on a non-object in
/opt/lampp/htdocs/test/MVC/Vue.php on
line 15

Here's my code:

Model.php:

class News {
    public function ConnBdd() {
        $this->bdd = new PDO('mysql:host=localhost;dbname=db301591273', 'root', '');
        $this->query = "SELECT Nom,IdTest,Image,DATE_FORMAT(DateCreation, '%Y-%m-%d') AS DateCreation FROM Questionnaires WHERE autorise='1' ORDER BY DateCreation DESC LIMIT 0, 4";
        $this->preparedQuery = $this->bdd->prepare($this->query);
        $this->executedQuery = $this->preparedQuery->execute();
    }
 }

Controller.php

class Controller {
    public $Model;
    public $View;
    public $News;
    public function ShowNews(){
        $this->News->ConnBdd();
        $this->View->ShowDaNews();
    }
}

View.php

class View {
    public $Model;
    public $News;

    public function ShowDaNews() {
        while ($c = $this->News->executedQuery->fetch()) {?>
    <tr>
    <td class="tableImg"><?echo '<img src="/img/ico/'.$tests['Image'].'.png" />'?></td>
    <td class="tableTest"><?echo '<a href="/page/php?t='.$tests['IdTest'].'">'.$tests['Nom'].'</a>'?></td>
    </tr>
            <?}
       }
}

and Index.php

require_once 'Modele.php';
require_once 'Controleur.php';
require_once 'Vue.php';

$Model= new Model();
$Controller = new Controller();
$View = new View();
$Controller->Model = $Model;
$Controller->View = $View;
$News = new News();
$Controller->News = $News;
$View->News = $News;
$Controller->ShowNews();

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身狗的梦 2024-09-23 07:06:09

扩展Robin的答案,$stmt->execute()的返回值是一个布尔值,指示语句是否执行成功。

另一方面,如果您正在进行面向对象编程,则应该设置 PDO::ERRMODE 设置,以便在语句失败时抛出异常:

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

请参阅 http://php.net/manual/en/pdo.setattribute.php

To expand on Robin's answer, the return value of $stmt->execute() is a boolean, indicating whether the statement executed successfully.

On another note, if you're doing object-oriented programming, you should set the PDO::ERRMODE setting, so that when statements fail exceptions will be thrown:

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

See http://php.net/manual/en/pdo.setattribute.php

伤感在游骋 2024-09-23 07:06:09

您误解了 PDO 中的准备好的语句,在新闻类中,将其切换为:

$this->preparedQuery = $this->bdd->prepare($this->query);
$this->executedQuery = $this->preparedQuery->execute();

...到此...

$this->preparedQuery = $this->bdd->prepare($this->query);
$this->preparedQuery->execute();
$this->executedQuery = $this->preparedQuery;

You've misunderstood prepared statements in PDO, in the class News, switch this:

$this->preparedQuery = $this->bdd->prepare($this->query);
$this->executedQuery = $this->preparedQuery->execute();

...to this...

$this->preparedQuery = $this->bdd->prepare($this->query);
$this->preparedQuery->execute();
$this->executedQuery = $this->preparedQuery;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文