duanqiu9104 2013-03-09 13:24
浏览 41
已采纳

PHP:在非对象上调用成员函数prepare()[复制]

This question already has an answer here:

My PHP function is very simple. It's just using prepared statements to query the database, but I can't understand why it's not working.

I keep getting a 'Call to a member function prepare() on a non-object' error, even though I have declared $conn as a private variable and using $this->conn

<?php

require_once 'constants.php';

class Mysql {

    private $conn;

    function ___construct(){

        $this->conn = new mysqli (DB_SEVER, DB_USER, DB_PASSWORD, DB_NAME);
        if (!$this->conn){
            die('There was a problem connecting to the database!');
        }

    }

    function verify_name_and_pass ($un, $pwd) {

        $query = "SELECT * FROM users 
                  WHERE username = ? AND user_password = ?
                  LIMIT 1";

        if ($stmt = $this->conn->prepare($query)) {

            $stmt->bind_param('ss', $un, $pwd);
            $stmt->execute();

            if ($stmt->fetch()) {
                $stmt->close();
                return true;            
            }

        }       
    }   
}
</div>
  • 写回答

1条回答 默认 最新

  • duanke2503 2013-03-09 13:30
    关注

    The problem is your __construct() function. There is at least one typo:

    • __construct() is spelled with 2 _, you spelled it with 3.
    • You probably meant to use DB_SERVER instead of DB_SEVER.

    Also, once you established that $this->conn is not null, you should check that the connection is correctly established by reading $this->conn->connect_errno and making sure it's 0.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 asp.textbox后台赋值前端不能显示什么原因
  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误