我遇到了我创建的数据库类的问题。 当我尝试显示表中的数据时,没有数据显示,但它仍然通过数据库 - > select()。
<?php
class database {
private $DBhost = "localhost";
private $DBuser = "username";
private $DBpass = "password";
private $DBname = "database";
private $PDO = null;
private $stmt = null;
function __construct() {
$this->PDO = new PDO("mysql:host=" . $this->DBhost . ";dbname=" . $this->DBname . ";charset=utf8", $this->DBuser, $this->DBpass);
}
function __destruct() {
$this->PDO = null;
}
function select($query, $param = array()) {
try {
$this->stmt = $this->PDO->prepare($query);
if (count($param) != 0) {
$this->stmt->execute($param);
} else {
$this->stmt->execute();
}
} catch(Exception $ex) {
$this->error($ex);
return false;
}
}
function resultset() {
return $this->stmt->fetchAll();
}
function error($ex) {
die('Something broke :(');
}
}
?>
我正在尝试使用它来显示html表中的数据,如下一页所示。
<?php
include 'code/db.php';
$DB = new database();
?>
<html>
<head>
<title>List</title>
</head>
<body>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone number</th>
<th>Mobile number</th>
<th>Address</th>
<th>State</th>
<th>Post Code</th>
<th>Settings</th>
</tr>
<?php
$DB->select('SELECT * FROM owners ORDER BY first_name, last_name');
while($line = $DB->resultset()) {
echo "<tr>";
echo "<td>" . $line['first_name'] . "</td>";
echo "<td>" . $line['last_name'] . "</td>";
echo "<td>" . $line['email'] . "</td>";
echo "<td>" . $line['phone_number'] . "</td>";
echo "<td>" . $line['mobile_number'] . "</td>";
echo "<td>" . $line['address'] . "</td>";
echo "<td>" . $line['state'] . "</td>";
echo "<td>" . $line['post_code'] . "</td>";
echo '<td><a href="item-list?id=' . $line['id'] . '">Details</a><a href="edit-owner?id=' . $line['id'] . '"</a></td>';
echo "</tr>";
}
?>
</table>
</body>
</html>
如果你能帮我解决这个问题,那就太好了。
答案 0 :(得分:1)
PDO已经是一个数据库类,你不需要任何其他类。特别是这样一个有状态的人。你只是在这个无用的课上挖了一个巨大的陷阱,很快就会陷入其中。
所以,保持原始PDO:
<?php
$DBhost = "localhost";
$DBuser = "username";
$DBpass = "password";
$DBname = "database";
$dsn = "mysql:host=$DBhostdbname=$DBname;charset=utf8";
$PDO = new PDO($dsn, $DBuser, $DBpass);
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
您实际需要的是将SQL与HTML分开。因此,首先获取所有数据然后使用任何模板输出它。
<?php
include 'code/db.php';
$stm = $PDO->query('SELECT * FROM owners ORDER BY first_name, last_name');
$data = $stm->fetchAll();
?>
<html>
<head>
<title>List</title>
</head>
<body>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone number</th>
<th>Mobile number</th>
<th>Address</th>
<th>State</th>
<th>Post Code</th>
<th>Settings</th>
</tr>
<?php foreach($data as $line):?>
<tr>
<td><?=$line['first_name']?></td>
<td><?=$line['last_name']?></td>
<td>
<a href="item-list?id=<?=$line['id']?>">Details</a>
<a href="edit-owner?id=<?=$line['id']?>Owner</a>
</td>
</tr>
<?php endforeach ?>
</table>
</body>
</html>
答案 1 :(得分:0)
您在fetchAll
功能中使用resultset
。将返回整个选定值数组并将其存储在$line
中。然后你必须迭代它。
将功能更改为:
function resultset() {
return $this->stmt->fetch( PDO::FETCH_ASSOC );
}
答案 2 :(得分:0)
更改此行
while($line = $DB->resultset()) {
到
foreach($DB->resultset() as $line ) {