无法使用 php MVC 从数据库中检索数据

Cannot Retrieve data from database With php MVC

我想使用自定义 PHP MVC 从数据库中检索一些数据。

我创建了一个控制器文件

semestres.php

class Semestres extends Controller {

    function __construct(){
        parent::__construct();      
    }
    public function index(){
        $this->view->fetchSemestres = $this->model->fetchSemestres();
        $this->view->render('semestres/index');
    }
}

一个型号

semestres_model.php

class Semestres_Model extends Model{
    public function __construct(){
        parent::__construct();
    }

    public function fetchSemestres(){
        $stmt = $this->db->prepare("SELECT * FROM semestres");  
        $stmt->setFetchMode(PDO::FETCH_ASSOC);  
        $stmt->execute();
        return $stmt->fetchAll();       
    }   
}

和一个 View 文件 index 在文件夹 semestre.

foreach($this->fetchSemestres AS $key=>$value){
    echo $value['intitule_semestre']."<br>";
}

但它什么也没给我!!!

print_r($this->fetchSemestres) 给我一个空数组

Array ( )

//更新:

数据库连接:

class Database extends PDO{
    public function __construct(){
        parent::__construct('mysql:host:localhost;dbname:planingexams', 'root', '');
    }
}

Model.php

class Model{

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

试试这个

class Database extends PDO{
    public function __construct(){
        parent::__construct('mysql:host=localhost;dbname=planingexams', 'root', '');
    }
}

我在本地测试了你的代码并添加了

var_dump($stmt->errorInfo());

这给了我:

array(3) {
  [0] =>
  string(5) "00000"
  [1] =>
  int(1046)
  [2] =>
  string(20) "No database selected"
}

所以在仔细查看数据库连接后,您似乎必须使用 = 而不是 : 来分配值。

参见:

$dbh = new PDO("pgsql:host=$host;port=5432;dbname=$db;user=$user;password=$pass");

来自

http://php.net/manual/en/class.pdostatement.php