doumu9799 2014-10-17 20:59
浏览 38
已采纳

如何连接到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?

  • 写回答

3条回答 默认 最新

  • duan0714 2014-10-17 21:19
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了