【发布时间】:2016-12-11 07:51:06
【问题描述】:
我一直在尝试学习如何在 MVC 结构中使用 php 类进行数据库操作。这是一个连接数据库的简单数据库类。
<?php
class Database
{
private $connect;
private $host, $username, $password, $databaseName;
public function __construct($host, $username, $password, $databaseName)
{
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->databaseName = $databaseName;
$this->connect = new mysqli($this->host, $this->username, $this->password, $this->databaseName);
if ($this->connect->connect_error) {
die('Connect Error (' . $this->connect->connect_errno . ') '. $this->connect->connect_error);
}
return true;
}
public function __destruct()
{
$this->connect->close();
}
}
实例化连接类
$db = new Database("localhost", "root", "", "framework");
到这里就好了,如果有错误,它会连接并显示和错误。
现在我在数据库中有一个表'Members',其中包含三列 1. Id、2. Name 和 3. Email 并在 members 表中显示记录 我在模型文件夹(MVC)中有一个成员模型文件。我试图像这样扩展数据库类
class Members extends Database {
public function getMembers() {
$result = $this->connect->query("SELECT * FROM members");
return $query->row;
}
}
我无法(过去 24 小时)做的是显示结果。
附:即使你能建议我一个关于如何扩展数据库类的教程也会很棒。
【问题讨论】:
-
$query中的Members是什么? -
@RC.,成员表中有记录,我正在尝试显示所有记录。
标签: php mysql database class oop