Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求大神纠错,谢谢Fatal error: Call to a member function query() on a non-object

求大神纠错,谢谢Fatal error: Call to a member function query() on a non-object

Jun 23, 2016 pm 01:46 PM

如题,出现错误。Fatal error: Call to a member function query() on a non-object in D:\WWW\2shou\class\Bulletin.php on line 47
代码如下: 
//本类用于保存对表Bulletin的数据库访问操作
//表的每个字段对应类的一个成员变量
Class Bulletin
{
  public $Id; // 记录编号
  public $Title; // 公告标题
  public $Content; // 公告内容
  public $PostTime; // 发布日期
  public $Poster; // 发布人
  var $conn;

  function __construct() {
// 连接数据库
$this->conn = mysqli_connect("localhost", "root", "pass", "2shou"); 
mysqli_query($this->conn, "SET NAMES gbk");
  }

  function __destruct() {
// 关闭连接
mysqli_close($this->conn);
  }

  // 获取公告信息
  function GetBulletinInfo($bid)  {
//设置查询的SELECT语句
  $sql = "SELECT * FROM Bulletin WHERE Id='" . $bid . "'";
// 打开记录集
    $results = $this->conn->query($sql);
// 读取公告数据
if($row = $results->fetch_row())  {
     $this->Id = $bid;
  $this->Title = $row[1];
  $this->Content = $row[2];
  $this->PostTime = $row[3];
      $this->Poster = $row[4];
}
else {
  $this->Id=0;
}
  }

  // 获取所有公告信息,返回结果集
  function GetBulletinlist()  {
//设置查询的SELECT语句
$sql = "SELECT * FROM Bulletin ORDER BY PostTime DESC";
    $results = $this->conn->query($sql);
    return $results;
  }

  // 获取所有公告信息,返回结果集
  function GetRecentBulletinlist()  {
//设置查询的SELECT语句
$sql = "SELECT * FROM Bulletin WHERE DateDiff(day, getdate(), Posttime)     $results = $this->conn->query($sql);
    return $results;
  }

  // 添加公告信息
  function insert()  {
    $sql = "INSERT INTO Bulletin (Title, Content, PostTime, Poster) VALUES ('" . $this->Title . "','" . $this->Content . "','" . $this->PostTime . "','" . $this->Poster . "')";
// 执行SQL语句
$this->conn->query($sql);
  }

  // 修改公告信息
  function update($bid)  {
    $sql = "UPDATE Bulletin SET Title='" . $this->Title . "', Content='" . $this->Content . "', PostTime='" . $this->PostTime . "', Poster='" . $this->Poster . "' WHERE Id=" . $bid;
    // 执行SQL语句
$this->conn->query($sql);
  }

  // 批量删除公告信息
  function delete($bid)  {
    $sql = "DELETE FROM Bulletin WHERE Id IN (" . $bid . ")";
    // 执行SQL语句
$this->conn->query($sql);
  }
}
?>


回复讨论(解决方案)

你在构造函数中
 function __construct() {
    // 连接数据库
    $this->conn = mysqli_connect("localhost", "root", "pass", "2shou"); 
    mysqli_query($this->conn, "SET NAMES gbk");
 }
所以 $this->conn 是资源
但你在其他方法中却用 $results = $this->conn->query($sql); 进行访问
那么 $this->conn 什么时候又变成对象了呀?

所以构造函数应写作

function __construct() {  $this->conn = new mysqli("localhost", "root", "pass", "2shou");  $this->conn->set_charset('gbk');}
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to Register and Use Laravel Service Providers How to Register and Use Laravel Service Providers Mar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles