gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:44:56 25 4
gpt4 key购买 nike

我的脚本一切正常,但数据没有出现在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com