【问题标题】:Error on prepare statement in php custom class mysqliphp自定义类mysqli中的prepare语句出错
【发布时间】:2014-01-13 18:56:19
【问题描述】:

请允许我使用OOPmysqli 我是新的,这是我用于数据库连接的 php 自定义类

class mysqldbconnect {
        protected $mysqli;
        public function __constructor() {
            $this->mysqli = new mysqli('localhost','root','','todo');
        }
        public function select_query() {
            $stmt = $this->mysqli->prepare("SELECT * FROM dos");
            $stmt->execute();
            $stmt->bind_result($id,$task,$sday,$lday,$des);
            $stmt->store_result();
            $count = $stmt->num_rows();
            if ( $count > 0 )
            {
                while ( $stmt->fetch()) {
                    echo $task." ".$sday." ".$lday." ".$des;
                }
            } else {
                echo "Nothing to do :) its great..";
            }
        }
    }

在这一行

    $stmt = $this->mysqli->prepare("SELECT * FROM dos");

我收到以下错误

    Fatal error: Call to a member function prepare() on a non-object

我在select_queryfunction 中尝试了var_dump 函数来检查$this-.mysqli 并且它返回NULL 所以错误显然现在我不知道如何删除这个错误? 请帮助我,我知道它的愚蠢问题。

【问题讨论】:

  • 你要调用select_query的代码是什么?
  • $db = new mysqldbconnect(); $db->select_query(); @佩里

标签: php mysql mysqli prepared-statement


【解决方案1】:

我看到的那段代码最大的问题是你没有构造函数。

应该是public function __construct() { 不是 __constructor

也就是说,您应该进行一些错误检查以确保成功创建 mysqli 对象。

【讨论】:

  • 这里很常见,而且总是很难发现,也很难调试。
  • 同意。有另一双眼睛看着它总是好的:)
  • 另一个是当一个 OP 有__contruct --- 那个总是需要我一分钟才能赶上。
  • 感谢 @mituw16 先生,现在一切正常,这是我最大的错误 :)
  • 对于其他人在将 __constructor 更正为 __construct 现在一切正常
猜你喜欢
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-16
  • 2018-06-13
相关资源
最近更新 更多