2014-10-17 149 views
0

我有一個登錄腳本,但是當我進入它有COM一個錯誤:如何連接到庫MySQLi服務器

Undefined property: Users::$host in C:\wamp\www\userlogin\classes\class.database.php on line 8

有4個文件:


<?php 
session_start(); 
include "classes/class.users.php"; 
if(isset($_POST['login'])) { 
$username = $_POST['username']; 
$password = $_POST['password']; 
$users->login($username, $password); 
} 
?> 
<!DOCTYPE html> 
<head> 
<title>Basic Login Script</title> 
</head> 
<body> 
<form method="POST" action="" name="login"> 
<input type="text" name="username"> 
<input type="password" name="password"> 
<input type="submit" name="login" value="Login"> 
</form> 
</body> 
</html> 



<?php 
class Database 
    { 
     public function __construct() 
      { 
       $host = 'localhost'; 
       $user = 'root'; 
       $pass = 'password'; 
       $name = 'usersystem'; 
       $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name); 

       if ($mysqli->connect_errno) 
       echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 

       echo $mysqli->host_info . "\n"; 
      } 
    } ?> 

<?php 
    include "class.database.php"; 

    class Users extends Database 
     { 
      public function login($username, $password) 
       { 
        $stmt = $this->mysqli->prepare("SELECT username, password FROM users WHERE username = ? and password = ? LIMIT 1"); 
        $stmt->bind_param('ss', $username, $password); 
        $stmt->execute(); 
        $stmt->bind_result($username, $password); 
        $stmt->store_result(); 
        if($stmt->num_rows == 1) { 
          while($stmt->fetch()) { 
            $_SESSION['username'] == $username; 
            header("Location: dashboard.php"); 
           } 
         } 
        else 
         return false; 

       $stmt->close(); 
       $stmt->free_result(); 
      } 
     } 

    $users = new users(); ?> 

//dashboard 
<?php echo "error"; ?> 

我用localhost/index.php運行,3個文件class.database.phpclass.users.phpdahsboard.php是在目錄:類
Mybe這是一個語法錯誤,但我不能找到它。 我已經在phpmyadmin中創建了一個數據庫並插入了數據。

任何人都可以幫助我嗎?

在數據庫改變$用戶__construct方法這個 - $>用戶,$主機這個 - $>主機等
+0

如果我是你,我會去C:\ wamp \ WWW \用戶登陸\類\ class.database.php和第8行。 – ElSS 2014-10-17 21:04:54

+0

'mysqli'不是服務器。它是PHP語言的「MySQL改進」擴展,允許您訪問MySQL服務器。 – Kamil 2014-10-17 21:23:12

回答

1

不能使用$this的局部變量,他們將需要類的屬性,你需要一個公衆一個用於連接,像這樣:

<?php 
class Database { 
    public $mysqli; 
    private $host = 'localhost'; 
    private $user = 'root'; 
    private $pass = 'password'; 
    private $name = 'usersystem'; 

    public function __construct() { 

     $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name); 
     if ($this->mysqli->connect_errno) { 
      echo "Failed to connect to MySQL: (". $this->mysqli->connect_errno . ") "; 
     }else{ 
      echo $this->mysqli->host_info . "\n"; 
     } 
    } 
} 
?> 

我注意到另一件事是你不」在設置之前,先開始一個會話。 你也應該重定向

if($stmt->fetch()) { 
    session_start(); 
    $_SESSION['username'] == $username; 
    header("Location: dashboard.php"); 
    exit; 
} 
+0

他也在混合使用'$ mysqli->'vs'$ this-> mysqli->' – Rasclatt 2014-10-17 21:21:53

+1

@Rasclatt是的,我剛剛在我的更新中添加了更多的問題,我認爲OP在這裏有很多問題 – meda 2014-10-17 21:23:51

+0

+1「 !」這裏有幾件比較珍貴的東西。 – Rasclatt 2014-10-17 21:24:40

1

試着改變你的數據庫連接到這一點:

class Database 
    { 
     // Since you are calling this variable in other methods 
     // you need to make it available. 
     public $mysqli; 
     public function __construct() 
      { 
       $host = 'localhost'; 
       $user = 'root'; 
       $pass = 'password'; 
       $name = 'usersystem'; 
       $this->mysqli = new mysqli($host, $user, $pass, $name); 
       // You are mixing local with class-wide variables. Should all conform. 
       if ($this->mysqli->connect_errno) 
        echo "Failed to connect to MySQL: (".$this->mysqli->connect_errno.")".$this->mysqli->connect_error; 

       echo $this->mysqli->host_info."\n"; 

      } 
    } 
0

退出後我已經改變了代碼class.datanase.php這樣

<?php 
class Database { 
public $mysqli; 
private $host = 'localhost'; 
private $user = 'root'; 
private $pass = 'password'; 
private $name = 'usersystem'; 

public function __construct() { 

    $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name); 
    if ($this->mysqli->connect_errno) { 
     echo "Failed to connect to MySQL: (". $this->mysqli->connect_errno . ") "; 
    }else{ 
     echo $this->mysqli->host_info . "\n"; 
    } 
} 
} 
?> 

現在到了錯誤:

解析錯誤:語法錯誤,意外'$ host'(T_VARIABLE),期望函數(T_FUNCTION)在C:\ wamp \ www \ userlogin \ classes \ class.database.php上4行