2014-10-27 57 views
0

我想如果用戶沒有登錄,顯示的數據,我發現了以下錯誤:成員函數錯誤。任何解釋如何解決

(Fatal error: Call to a member function Fetch() on a non-object in /home/a6150953/public_html/php/classes/class.user.php on line 58) Any help

class.users.php

<?php 
class User 
{ 
    public $db; 
    public $error; 

    public function __construct($con){ 
      $this->db = $con; 
    } 

    /*** for login process ***/ 
    public function check_login($username='', $password=''){ 
      // Validate that your email is a real one 
      if(filter_var($username,FILTER_VALIDATE_EMAIL) !== false) { 
        $password = md5($password); 
        $sql  = "SELECT uid from users WHERE (uemail='$username' or uname='$username') and upass = '$password'"; 
        $result  = $this->db->Fetch($sql); 

         if ($result !== 0) { 
           // this login var will use for the session thing 
           $_SESSION['emailusername'] = $result[0]['uemail']; 
           $_SESSION['uid']   = $result[0]['uid']; 
           $_SESSION['user']   = $this->get_fullname($result[0]['uid'],0); 
           $_SESSION['login']   = true; 
          } 
         else 
          $this->error['account'] = 'Invalid Username/Password'; 
       } 
      else 
       $this->error['email'] = 'Invalid Email Address'; 

      return (!isset($_SESSION['emailusername']))? false:true; 
     } 

    /*** for showing the username or fullname ***/ 
    public function get_fullname($uid, $write = 1){ 

      // --> You can prepare, bind, and execute your values here replacing what you have now....<-- 
      $sql    = "SELECT * FROM users WHERE uid = $uid"; 
      $user_data   = $this->db->Fetch($sql); 

      if($user_data !== 0) { 
        $user['fullname'] = $user_data['fullname']; 
        $user['uemail']  = $user_data['uemail']; 
        $user['uid']  = $user_data['uid']; 

        // This gives the option of returning an array (setting session array) or echoing 
        if($write == 1) 
         echo implode("<br />",$user); 
        else 
         return $user; 
       } 
     } 

    public function check_user($uid) 
     { 
      $sql  = "SELECT * from users WHERE uid='$uid'"; 
      $result  = $this->db->Fetch($sql); 
      $count_row = ($result !== 0)? count($result): 0; 

      return ($count_row == 1); 
     } 

    /*** starting the session ***/ 
    public function get_session() 
     { 
      return $_SESSION['login']; 
     } 

    public function user_logout() 
     { 
      $_SESSION['login'] = FALSE; 
      session_destroy(); 
     } 
} 

? >

profile.php

<?php 
session_start(); 
//session_destroy(); 

include_once('php/classes/db_config.php'); 
include_once('php/classes/class.user.php'); 

$user1 = new User(""); 

if(isset($_POST['logout'])) { 
     session_destroy(); 
     header('Location: index.php'); 
} 

include_once('php/common/head.php'); 

$all = $con->Fetch("select * from users"); 
?> 
<div class="wrapper"> 
<h1> 
<?php 
if(isset($_SESSION['uid'])){ 
    echo "Profile Page for ". $all[0]['fullname'] ." "; 
}else{ 
    echo "Welcome", "<br/><a href='index.php'>Login</a>"; 
} 
?> 
</h1> 
<pre> 
<div id="profile"> 
<?php 
if(isset($_GET['uid']) || isset($_SESSION['uid'])) { 

    if($_GET['uid'] === $_SESSION['uid']) { 

     echo '<p>'.$all[0]['fullname'].'</p>'; 
     echo '<p>'.$all[0]['uemail'].'</p>'; 

     echo "<form action='' method='post'> 
      <input type='hidden' name='logout' value='true' /> 
      <input type='submit' name='submit' value='Logout'> 
     </form>"; 

    }else if($user1->check_user($uid)){ 

     echo '<p>'.$all[0]['fullname'].'</p>'; 
     echo '<p>'.$all[0]['uemail'].'</p>'; 

    }else if(!$user1->check_user($uid)){ 
     echo "Invalid User"; 
    } 

}else{ 
    echo "Incorrect"; 
} 
?> 
</pre> 
</div> 
</div> 
<?php include_once('php/common/foot.php'); ?> 
+0

我已經決定要回滾編輯連接因爲你的編輯使問題成爲一個全新的問題,而答案卻無效。請[打開新的問題](http://stackoverflow.com/questions/ask) – 2014-10-27 15:44:57

回答

2

你構建User沒有有效數據庫的方法Fetch(8號線profile.php)

$user1 = new User(""); 

我會假設類似(沒有看到你的數據庫類)

$user1 = new User($con); 
+0

還有一件事?如果我可能 – 2014-10-27 15:38:08

+0

去吧:)要求離開 – 2014-10-27 15:38:29

+0

如果它不是根據你最初的問題,或不是一個快速的「爲什麼這樣工作?」問題,請考慮[問另一個問題](http://stackoverflow.com/questions/ask) – 2014-10-27 15:41:39