数据库类错误-php


Database Class Error - php

我创建的数据库类出现问题。当我尝试显示表中的数据时,没有显示任何数据,但它仍然通过数据库->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>

如果你能帮我解决这个问题,那就太好了。

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>

您在resultset函数中使用fetchAll。所选值的整个数组将被返回并存储在$line中。然后你必须对它进行迭代。

将功能更改为:

function resultset() {
  return $this->stmt->fetch( PDO::FETCH_ASSOC );
}

更改此行

while($line = $DB->resultset()) {

foreach($DB->resultset() as $line ) {