关于PHP和MVC的Beginner问题
我试图学习PHP的MVC架构。所以我玩一些简单的类和函数。我找不到这段代码有什么问题,它返回一个:关于PHP和MVC的Beginner问题,php,pdo,Php,Pdo,我试图学习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=db30159
致命错误:对中的非对象调用成员函数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>
<?}
}
}
感谢您的帮助。您误解了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;
你误解了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;
要扩展Robin的答案,返回值$stmt->execute()是一个布尔值,指示语句是否成功执行 另一方面,如果您正在进行面向对象编程,则应设置PDO::ERRMODE设置,以便在语句失败时抛出异常:
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
请参见展开Robin的答案,$stmt->execute()的返回值是一个布尔值,指示语句是否成功执行 另一方面,如果您正在进行面向对象编程,则应设置PDO::ERRMODE设置,以便在语句失败时抛出异常:
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
请看很高兴我停止使用PHP。。。我讨厌这垃圾。很高兴我停止使用PHP。。。我讨厌那种垃圾。