从item.php可以看出,我有2个功能来显示一些项目
display_item_1()和display_item_2()
当我通过调用
运行程序时$item = new item();
$item->display_item_1(1);
$item->display_item_2(1);
它只显示function-> display_item_1()
但是对于display_item_2()收到致命错误:在null上调用成员函数query()
换句话说,函数display_item_1()和display_item_2()都是相同的编码,但只显示第一个。 那么这意味着item.php中的构造函数不正确?
或者我做错了哪一部分?
db.php中
class db{
protected $db_host;
protected $db_name;
protected $db_user_name;
protected $db_pass;
public function __construct() {
$this->db_host="localhost";
$this->db_name="bs";
$this->db_user_name="root";
$this->db_pass="";
}
public function conn(){
try {
$conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user_name="root", $this->db_pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
item.php
require "db.php";
class item {
public $user_uid;
protected $item_name;
public $conn;
public function __construct(){
$db = new db();
$this->conn= $db->conn();
}
public function display_item_1($uid){
$this->user_uid=$uid;
try{
$sql="SELECT * FROM item where uid='$this->user_uid'";
$statement=$this->conn->query($sql);
}
catch(PDOException $e){
...
}
}
public function display_item_2($uid){
$this->user_uid=$uid;
try{
$sql="SELECT * FROM item where uid='$this->user_uid'";
$statement=$this->conn->query($sql);
}
}
catch(PDOException $e){
...
}
}
}
答案 0 :(得分:1)
在你的另一个问题中也提到过,我将在这个问题中再次提到它:重写你的代码并让DB类处理查询。现在你的数据库类在面向对象的设计方面没有意义,它编写得不好,应该从头开始重写。以下只是一个想法:
<强> db.php中强>
// Use uppercase for class names and stick to possibly unreserved names
class DatabaseHandler
{
// moved from constructor and set to private, you are surely not using inheritance
private $db_host="localhost";
private $db_name="bs";
private $db_user_name="root";
private $db_pass="";
private $connection= FALSE; // added, holds db connection
/* obsolete in your case
public function __construct() { }
*/
public function connect()
{
try {
$conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user_name="root", $this->db_pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection= $conn;
return TRUE;
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
return FALSE;
}
}
/**
* return records from your database; checks skipped in this example
*/
public function query($query)
{
// only query if a database connection is available, else return false
if (!$this->connection) return FALSE;
try {
$statement=$this->connection->query($query);
return $statement->fetchAll();
}
catch(PDOException $e){
// logging or whatever ...
return FALSE;
}
}
}
<强> item.php 强>
class Item {
public $user_uid;
private $item_name;
private $conn; // why make it public?
public function __construct(){
$db = new DatabaseHandler();
if (!$db->connect()) die("Database not available"); // update these lines
$this->conn= $db;
}
public function display_item_1($uid){
$this->user_uid=$uid;
$arrValues= $this->conn->query("SELECT * FROM item where uid='$this->user_uid'");
print_r($arrValues);
}
public function display_item_2($uid){
$this->user_uid=$uid;
$arrValues= $this->conn->query("SELECT * FROM item where uid='$this->user_uid'");
print_r($arrValues);
}
}
运行代码:
$item = new Item();
$item->display_item_1(1);
$item->display_item_2(1);
请仔细阅读一些教程,尤其是类和OOP(面向对象编程)。