从sqlite3数据库在php中创建表

时间:2015-03-15 01:07:45

标签: php html database sqlite

我正在尝试使用php创建一个html表来从SQLite3数据库中检索数据。该数据库包含一个用户和账单表,每行有关于每行的信息。我试图显示表但页面将无法加载,我无法弄清楚我的代码有什么问题。任何帮助将不胜感激,谢谢!

我的数据库架构:

CREATE TABLE users(id integer primary key, Username varchar(15), FirstName varchar(15), LastName varchar(15), Email varchar(50), HouseName varchar(20), Phone integer, salt varchar(50), Encrypted_password varchar(50));
CREATE TABLE bills(id integer primary key, HouseName varchar(20), Bill varchar(20), Amount real, PaidBy varchar(20), Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

html / php table:

<table>
  <thead>
<tr>
  <th width="100"> Bill</th>
  <th width="100">Amount </th>
  <th width="100">Paid By</th>
  <th width="100">Added On</th>
  <th width="100">You owe</th>
</tr>
  </thead>
  <tbody>
<?php
  require ‘session.php’; 
  require ‘database.php’;
  while(($row = $querybills->fetchArray())) {
  $html .= "<tr><td>"$row['Bill']"</td><td>"$row['Amount']"</td><td>"$row['PaidBy']"</td><td>"$row['Date']"</td><td>"$row['Amount'] / $billscount"</td>   </tr>";
  }
  echo $html;
?>
  </tbody>
</table>

session.php文件:

<?php
session_start();
require 'database.php';
if(isset($_SESSION['id'])) {
  $db = new Database();
  $queryuser = "SELECT * FROM users WHERE(id = '".$_SESSION['id']."')";
  $user = $db->querySingle($queryuser);

  $querybills = $db->query("SELECT * FROM bills WHERE(HouseName == ".$user['HouseName'].")");
  $bill = $db->querySingle($querybills);
  $billscount = $db->query("SELECT COUNT(*) FROM users WHERE(HouseName == ".$user['HouseName'].")");
} else {
  header('Location:login.php');
}
?>

database.php中:

<?php
class Database {

private $database;

function __construct() {
    $this->database = $this->getConnection();
}

function __destruct() {
    $this->database->close();
}

function exec($query) {
    $this->database->exec($query);
}

function query($query) {
    $result = $this->database->query($query);
    return $result;
}

function querySingle($query) {
    $result = $this->database->querySingle($query,true);
    return $result;
}

function prepare($query) {
    return $this->database->prepare($query);
}

function escapeString($string) {
    return $this->database->escapeString($string);
}

private function getConnection() {
    $conn = new SQLite3('bills.db');
    return $conn;
}
}
?>

1 个答案:

答案 0 :(得分:0)

您需要在循环外初始化$ html,因为它只在while范围内...除非您在所需文件中定义它。

因此您需要在外部对其进行初始化,因此echo语句可以看到它。

另外......与

一致
$html .= "<tr><td>"$row['Bill']"</td><td>"$row['Amount']"</td><td>"$row['PaidBy']"</td><td>"$row['Date']"</td><td>"$row['Amount'] / $billscount"</td>   </tr>";

您没有将变量(使用.运算符)连接到字符串。

更改为:

$html .= "<tr><td>" . $row['Bill'] . "</td><td>" . $row['Amount']" . </td><td>" . $row['PaidBy'] . "</td><td>" . $row['Date'] . "</td><td>" . ($row['Amount'] / $billscount) ."</td>   </tr>";

所以你的新代码可以

<table>
  <thead>
<tr>
  <th width="100"> Bill</th>
  <th width="100">Amount </th>
  <th width="100">Paid By</th>
  <th width="100">Added On</th>
  <th width="100">You owe</th>
</tr>
  </thead>
  <tbody>
<?php
  require ‘session.php’; 
  require ‘database.php’;

    $html = '';


    while(($row = $querybills->fetchArray())){
        $html .= "<tr><td>" . $row['Bill'] . "</td><td>" . $row['Amount'] . "</td><td>" . $row['PaidBy'] . "</td><td>" . $row['Date'] . "</td><td>" . ($row['Amount'] / $billscount) ."</td>   </tr>";
    }
    echo $html;

?>
  </tbody>
</table>