从数据库中检索的信息不会显示?

时间:2015-12-03 01:10:26

标签: php model-view-controller pdo

我正在使用PHP(MVC)来构建一个网站。我已经使用PDO成功连接并将数据插入到SQL数据库中。但是,我遇到了回收客户数据的问题,我没有发现任何错误。只是信息没有从View显示,我已经为客户定义了一个类,如下所示:

<?php

 class CustomerDetails {


    private $_name,$_email,$_phone, $_choise;

    public function __constructor($dbrow){
        $this->_name = $dbrow['name'];
        $this->_email = $dbrow['email'];
        $this->_phone = $dbrow['phone'];
        $this->_choise = $dbrow['choise'];
                }
    function get_name() {
        return $this->_name;
    }

    function get_email() {
        return $this->_email;
    }

    function get_phone() {
        return $this->_phone;
    }

    function get_choise(){
        return $this->_choise;
    }      
}

我还定义了一个用于执行SQL查询的类,该类查询客户详细信息并将其存储在数组列表中,如下所示:

<?php
 require_once ('Models/CustomerDetails.php'); 
require_once ('Models/Database.php'); 

class AdminPanel{


 public function __construct() { 
 $this->_dbInstance = Database::getInstance(); 
 $this->_dbHandle = $this->_dbInstance->getdbConnection(); 
 } 

 public function fetchAllCustomers() { 
 $sqlQuery = 'SELECT * FROM info'; 
 echo $sqlQuery; //helpful for debugging to see what SQL query has been created 
 $statement = $this->_dbHandle->prepare($sqlQuery); // prepare PDO statement 
 $statement->execute(); // execute the PDO statement 

 $dataSet = []; 
 while ($row = $statement->fetch()) { 
 $dataSet[] = new CustomerDetails($row); 
 } 
 return $dataSet;
 echo $dataSet;
 } 

查看负责显示信息的位置,但会出现注意:

  <div class="row">
                    <div class="col-lg-6">
                        <h2>Customer Table</h2>
                        <div class="table-responsive">

                            <table class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th>Name</th>
                                        <th>Email</th>
                                        <th>Phone</th>
                                        <th>Choise</th>
                                    </tr>
                                </thead>
                                <tbody>

                                    <?php  foreach ($view->infos as $adminPanel) {

echo  '<tr>'  . '<td>' .    $adminPanel->get_name() . '</td>' 
             . '<td>' . $adminPanel->get_email() . '</td>'
             . '<td>' . $adminPanel->get_phone() . '</td>'
             . '<td>' . $adminPanel->get_choise() . '</td>'


                                           . '</tr>'; 
  } ?>                             
                                </tbody>
                            </table>
                        </div>
                    </div>     
    }

将视图对象传递给View的控制器:

require_once('Models/AdminPanel.php'); 
$view = new stdClass();

$adminPanel = new AdminPanel(); 
$view->infos  = $adminPanel->fetchAllCustomers();    //->fetchAllStudents(); 

require_once('Views/adminPanel.phtml');

DataBase连接类:

class Database {
protected static $_dbInstance = null;
protected $_dbHandle;
public static function getInstance(){
$host = 'localhost';
$dbName = 'oasis';
$username = 'root';
$password = '';

    if(self::$_dbInstance=== null) { //checks if the PDO exists, if not create it with
                                     //the connection info
        self::$_dbInstance= new self($username, $password, $host, $dbName);

        }
        return self::$_dbInstance;

    }

    private function __construct($username, $password, $host, $database) {
        try {
            $this->_dbHandle= new PDO("mysql:host=$host;dbname=$database", $username, $password); // creates database handle with connection info

            }
            catch (
                    PDOException$e) { // catch any failure to connect to database
                    echo $e->getMessage();

                    }

                    }
  public function getdbConnection(){
      return $this->_dbHandle; // returns the database handle to be used elsewhere

      }
      public function __destruct() {
          $this->_dbHandle= null; // destroys the destroys the database handle
          }
} 

我没有收到任何错误,只是信息没有显示。

0 个答案:

没有答案