成员函数错误。任何解释如何解决

时间:2014-10-27 15:28:03

标签: php oop

如果用户未登录并且我收到以下错误,我试图显示数据:

  

(致命错误:在第58行的/home/a6150953/public_html/php/classes/class.user.php中的非对象上调用成员函数Fetch())任何帮助

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();
        }
}

&GT;

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'); ?>

1 个答案:

答案 0 :(得分:2)

您正在构建User而没有使用方法Fetch的有效数据库连接(第8行profile.php)

$user1 = new User("");

我会假设(没有看到你的数据库类)

$user1 = new User($con);