从数据库获取信息

时间:2015-08-09 15:37:06

标签: php oop mysqli

我正在观看有关使用OOP的CMS教程 - PHP& RainTPL

控制页面上的

:( articles.php)

<?php
require_once('globals.php');
require_once(CONTROLLERS.'ArticlesController.php');

$articlesmodel = new ArticlesModel() ; 

$catsmodel     = new ArticlesCatsModel();

$controller    = new ArticlesController($articlesmodel,$catsmodel);

$controller->show();

?>

globals.php:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

define('ROOT',dirname(__FILE__));
define('INC',ROOT.'/includes/');
define('CORE',INC.'/core/');
define('MODELS',INC.'/models/');
define('CONTROLLERS',INC.'/controllers/');
define('LIBS',INC.'/libs/');

/*
core files
*/
require_once(CORE.'config.php');
require_once(CORE.'mysql.class.php');
require_once(CORE.'raintpl.class.php');
require_once(CORE.'system.php');

System::Store('db',new mysql());
System::Store('tpl',new RainTPL()); //class RainTPL
?>

ArticlesController.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);


require_once(MODELS.'ArticlesModel.php');
require_once(MODELS.'ArticlesCatsModel.php');


class ArticlesController
{

    private $articlesModel; //Articles Model Object
    private $articlesCatsModel; //Articles Cat Model Object
                               //object of ArticlesModel class
    public function __construct(ArticlesModel $articlesmodel,ArticlesCatsModel $catsmodel)
    {

        $this->articlesModel     = $articlesmodel ;
        $this->articlesCatsModel = $catsmodel ; 
    }


    public function Show()
    {
        // array of articles from model :D 
        /*
           he takes object from Articlesmodel.php
           like private articles model 
           then he  call the Get function 
           from  ArticlesModel Class 
        */

        $articles = $this->articlesModel->Get();
        $cats     = $this->articlesCatsModel->Get();


        //put them inside the template after getting them
        System::Get('tpl')->assign('articles',$articles)    ;   
        System::Get('tpl')->assign('cats',$cats)    ;   

        // show them in the templatee

        System::Get('tpl')->draw('blog');
    }

}

Blog Where articles should be shown

但博客

没有问题

问题是$articles = $this->articlesModel->Get();返回空白数组并不包含来自数据库的信息

ArticlesCatsModel

ArticlesCatsModel includes/model

ArticleModel

ِArticlesModel

3http://pastebin.com/z2dzcBVc即使DB中有行,它也会返回空白数组

这是模板我应该看到我从DB获得的文章

但结果是没有文章

检查问题的确切位置

我输入了

在Articles.Controller.php中

    $articles = $this->articlesModel->Get();
    var_dump($articles).'<br/>'; 
    $cats     = $this->articlesCatsModel->Get();
    print_r($cats); 

如果你在图片中注意到我调用$cats但我返回空白数组$articles

Template

1 个答案:

答案 0 :(得分:1)

尝试替换

SELETE * FROM 

SELECT * FROM 

在ArticlesModel.php文件中的Get()方法中。