注意:尝试在第25行的<file>中获取非对象的属性

时间:2017-02-10 19:13:15

标签: php html mysql

似乎无法在我的代码中看到错误。

“注意:试图在第25行获取非对象的属性” 错误。

第25行:if($results->num_rows == 0){

引用此代码:这是login.php

<?php
    session_start();
    require_once 'conn.php';
    $DB_SQL = new DB_con();
    ob_start();

    if( isset($_POST['btn-login']) ) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $error_msg = "";
    $pass_control = "";

      if ($username ==""){
      $error_msg .= "<b> fill username! </b> <br/>";
      }
      if ($password ==""){
      $error_msg .= "<b> fill password </b> <br/>";
      }


      if ($error_msg !=""){
        echo $error_msg;
      }else{
            $results = $DB_SQL->getUser($username);
            if($results->num_rows == 0){
            $error_msg = "wrong username or password";
            echo $error_msg;
            }else{
            foreach ($results as $rslt) {
              $pass_control = $rslt['password'];
              $_SESSION['name'] = $rslt['name'];
            }
            if($pass_control == md5($password)){
              header('location:index.php');
            } else {
              $error_msg = "wrong username or password";
              echo $error_msg;
            }
          }
        }
      }

?>

<div id="main">

    <div class="main_top">
            <h1>Login</h1>
        </div>

        <div class="main_body">

                <p>sign in.</p>

        <form method="post" >
        <input type="text" id="username" name="username" placeholder="Enter username" maxlength="15"  /> </br>
        <input type="password" id="pass" name="password" placeholder="Enter Password" maxlength="15"  />
        <button id="btn-login" name="btn-login"> Login </button>
        </form>
        <a href="register.php">Sign Up Here...</a>
      </div>

        <div class="main_bottom"></div>

</div>

  <?php
  $page_title = 'Home';
  $thisPage='Home';

  $mastermain= ob_get_contents();
  ob_end_clean();

  $masterlink ="";
  $masterhead ="";

  Include("master.php");

  ?>

这是 conn.php

<?php

    define('DB_SERVER', 'localhost');
    define('DB_USER', 'root');
    define('DB_PASS', '');
    define('DB_NAME', 'go');

    class DB_con {
        Private $conn;

        function __construct() {
            $this->conn = mysqli_connect (DB_SERVER,DB_USER,DB_PASS,DB_NAME) or die ('localhost Connection Failed'.mysqli_connect_error());
        }
        public function setUser ($fullname,$username,$password) {
            $res = mysqli_query($this->conn,"INSERT INTO users(fullname,username,password) VALUES('$fullname',$username,'$password')");
            return $res;
        }
        public function getUser ($username) {
            $res = mysqli_query($this->conn,"SELECT * FROM users Where username ='$username')");
            return $res;
        }

    }
?>

我做错了什么?!

the error

0 个答案:

没有答案