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

php中文网
发布: 2016-06-23 13:46:15
原创
1375人浏览过

如题,出现错误。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);
  }
}
?>

挖错网
挖错网

一款支持文本、图片、视频纠错和AIGC检测的内容审核校对平台。

挖错网 28
查看详情 挖错网

回复讨论(解决方案)

你在构造函数中
 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');}
登录后复制

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号