致命错误:在非对象上调用成员函数bind_param()

时间:2014-09-23 16:50:23

标签: php mysqli

我尝试从我的getData方法返回true,以便在通过$ _SESSION ['用户名']登录主页后重定向,但我总是得到上面的错误。如何使此方法返回true?

<?php

class Login
{
    private $host        = "localhost";
    private $user        = "root";
    private $db_password = "*****";
    private $database    = "*******";

    private $db;

    private $username;
    private $password;

    function __construct($username, $password)
    {
        // Set data
        $this->setData($username, $password);

        // connect to db
        $this->connectToDb();

        //get Data
        $this->getData();

    }

   private function setData($username, $password){

             $this->username = $username;
             $this->password = $password;

            }

    private function connectToDb(){
                      // connect to the server.
                   $this->db = new mysqli($this->host, $this->user, $this->db_password);    
                       if ($this->db->connect_errno) {
                           die("We are sorry, you could not be connected to the server,
                            plaese check your connection setting!");
                       }else{
                           echo "You are connected to the database";
                       }


            }

       private function getData(){
                $query ="SELECT id FROM users WHERE username = '$this->username' AND
                                                    password = '$this->password'";

                $stmt = $this->db->prepare($query);  
                $stmt->bind_param('ss', $this->username, $this->passowrd); //Hier is the error.
                $stmt->execute();
                $stmt->bind_result($username, $password);
                $stmt->fetch();
                $numberofrows = $stmt->num_rows();

                echo  '# rows: '.$numberofrows;

                if ($numberofrows>1) {
                     return true;
                }else{
                    throw new Exception("Please check your username and passowrd!");

                }

             }
        }
?>

我感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的查询不使用占位符,您应该使用

$query ="SELECT id FROM users WHERE username = '?' AND password = '?'";

example