2014-10-27 41 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