2014-10-17 153 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行