如何连接到MySQLi服务器

时间:2014-10-17 20:59:12

标签: php 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.php dahsboard.php位于目录中:classes
Mybe这是一个语法错误,但我找不到它。 我在phpmyadmin中创建了一个数据库并插入了数据。

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分: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";

            }
    }

答案 1 :(得分: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;
}

答案 2 :(得分:0)

在数据库的__construct方法中将$ user更改为$ this-&gt; user,$ host to $ this-&gt; host etc ..