2013-09-01 77 views
-1

我有一個問題,當我輸入登錄信息,我得到這個錯誤致命錯誤:調用一個非對象的成員函數prepare()。

Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\Remake Login Page\Classes\Mysql.php on line 22

的代碼Mysql.php

<?php 

require_once 'includes/constants.php'; 

class Mysql { 
private $conn; 

function _construct() { 
    $this->$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die 
        ('There was a problem connecting to the database'); 
} 

function verify_Username_and_Pass($un, $pwd) { 


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


    if($stmt = $this->conn->prepare($query)) { 
     $stmt->bind_param('ss', $un, $pwd); 
     $stmt->execute(); 


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

} 

} 

希望有人能幫助我。

在此先感謝。

+0

你的構造函數是錯誤的,應該__construct() – hank

+0

參見:http://stackoverflow.com/questions/5346186/pdo-call-to-a-member-function-準備在非對象 –

回答

3

您的構造函數需要兩個下劃線。

2

您的構造函數應該說$this->conn而不是$this->$conn

4

構造函數方法以雙下劃線開頭。帶有單個下劃線的方法只是正常的函數名稱。

試試這個:

function __construct() { 
    $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database'); 
} 
+0

感謝這工作:D – user2737015

+0

有什麼區別,雙下劃線? – Vector

+0

它是一個構造函數。單下劃線只是正常的功能名稱。 –

相關問題