如何连接到MySQLi服务器

如何连接到MySQLi服务器

问题描述:

I have a login-script, but when i proceed it there com a error:

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

There is 4 files:


<?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 . "
";
            }
    } ?>

<?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"; ?>

I use localhost/index.php to run and the 3 files class.database.php and class.users.php dahsboard.php is in the directory: classes
Mybe it is a syntax-error, but i can not locate it. I have created a database in phpmyadmin and inserted the data.

Can anybody help me?

You can't use $this for local variable, they will need to be property of the class, and you need a public one for the connection, like 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 . "
";
       }
    }
}
?>

Other thing I notice is you don't start a session before setting it. You should also exit after redirecting

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

in the __construct method for Database change $user to $this->user, $host to $this->host etc..

Try changing your database connection to this:

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."
";

            }
    }