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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?