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条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题