PHP PDO 类 : Data dosn't appear in my index. php

标签 php mysql pdo

我的脚本一切正常,但数据没有出现在我的 index.php 中: http://i.stack.imgur.com/9GWdj.png
http://i.stack.imgur.com/yxEdE.png

index.php:

<?php
header('Content-Type: text/html; charset=utf-8');
?>
<!doctype html>
<html>
<head>
    <title>DB Connextion Test</title>
    <style>
        table, th, td {
            border: 1px solid black;
            border-collapse: collapse;
        }
    </style>
</head>
<body>
<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>Nom</th>
        <th>Prenom</th>
    </tr>
    </thead>
    <tbody>
        <?php
        require ('Database.php');
        $db = new Database();
        $dataSet = $db->getUsers('SELECT * FROM `users`');
        if (!empty($dataSet)) {
            foreach ($dataSet as $data) {
                echo '<tr>';
                echo '<td>'.$data->getUserId().'</td>';
                echo '<td>'.$data->getUserNom().'</td>';
                echo '<td>'.$data->getUserPrenom().'</td>';
                echo '</tr>'; 
            }
        } else
            echo 'No Result Found!';
        ?>
    </tbody>
</table>
</body>
</html>

数据库.php:

<?php
require ('Users.php');

class Database {

    public $db_name = 'authsys_db';
    public $host = 'localhost';
    public $user = 'root';
    public $password = '';
    public $con = '';
    public $db_handle = '';

    public $testMode = TRUE;

    public function __construct () {
        $this->con = sprintf('mysql:dbname=%s;host=%s', $this->db_name, $this->host);
        if ($this->testMode) {
            $this->db_handle = new PDO($this->con, $this->user, $this->password,
                                       array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
        } else {
            $this->db_handle = new PDO($this->con, $this->user, $this->password);
        }
    }

    public function  getUsers ($sql) {
        $stmt = $this->db_handle->prepare($sql);
        $stmt->execute();
        while ($row = $stmt->fetch()) {
            $dataSet[] = new Users($row);
        }
        if (!empty($dataSet))
            return $dataSet;
        else 
            return NULL;
    }

}
?>

用户.php:

<?php

class Users {

    private $id;
    private $nom;
    private $prenom;

    public function __construnct ($row) {

        $this->id = $row['ID'];
        $this->nom = $row['nom'];
        $this->prenom = $row['prenom'];
    }

    public function getUserId () {
        return $this->id;
    }

    public function getUserNom () {
        return $this->nom;
    }

    public function getUserPrenom () {
        return $this->prenom;
    }
}

?>

在我的数据库中,用户表中只有一行,我得到了该行,但它是空的,我真的不知道在这里做什么...一点帮助!!

最佳答案

您有一个拼写错误,请将 __construnct 更改为 __construct :

public function __construnct ($row) {
     $this->id = $row['ID'];
     $this->nom = $row['nom'];
     $this->prenom = $row['prenom'];

}

确实,您有一个用户,但由于此拼写错误,他的所有属性都是空的。此外,通过在 __construct() 中设置所有属性来创建用户可能不是最好的方法。

关于PHP PDO 类 : Data dosn't appear in my index. php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25585989/

相关文章:

php - 你能告诉 PHP 在发生错误时发送 header (如 500)吗?

php - 如何从源文件构建 WINCACHE .DLL?

MySQL 按 Where 子句匹配的顺序排序结果

mysql - codeigniter 交易 - trans_start trans_complete trans_status

MySQL 案例/如果/那么

PHP PDO 不更新表并且不产生任何错误

php - 使用 jquery ajax 删除表中的记录

php - 为什么 (If false return "true") ... 返回 true?

php - Laravel 4.2 从选择中插入

php - 如何获取插入行 PDO PHP 的自增主键?