获取错误Mysql.PDO


Getting error Mysql. PDO

我创建了一个profile.php页面,但在登录到我的用户配置文件后,我想显示用户信息。我得到以下错误。

错误消息致命错误:在第16行对/home/a6150953/public_html/profile.php中的非对象调用成员函数Fetch()`任何人都可以帮我找到解决方案。

profile.php

  <?php
    session_start();
    //session_destroy();
    include_once('php/classes/class.user.php');
    $user1 = new User($con);
    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 " " . $all[0]['fullname'] . "  ";
            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'); ?>

DB_CONFIG.php

<?php
    class   DBEngine
        {
            public  $con;
            public  $errors;
            public  function __construct("LOGIN CREDENTIALS CANT SHOW")
                {
                    try {
                            $this->con  =   new PDO("mysql:host=$host;dbname=$db",$user,$pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
                        }
                    catch (Exception $e) {
                          $this->errors['connect']['message']    =   $e->getMessage();
                          $this->errors['connect']['error_code'] =   $e->getCode();
                        }
                }
                public  function Fetch($_sql)
                {
                    $query  =   $this->con->prepare($_sql);
                    $query->execute();
                    $this->errors['fetch'][]    =   $query->errorInfo();
                    if($query->rowCount() > 0) {
                            while($rows = $query->fetch(PDO::FETCH_ASSOC)) {
                                    $array[]    =   $rows;
                                }
                        }
                    return (isset($array) && $array !== 0 && !empty($array))? $array: 0;
                }
            // Simple write to db method
            public  function Write($_sql)
                {
                    $query  =   $this->con->prepare($_sql);
                    $query->execute();
                    $this->errors['insert'][]   =   $query->errorInfo();
                }
        }
// To use/initialize
$con = new DBEngine(); ?>

CLASS.USER.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();
            }
    }
?>

更新:

仍然出现错误::::Call to undefined method USER::FETCH() in line 16

您需要包含DBEngine class页面并仍然使用$con

include_once('php/classes/DB_CONFIG.php');

您只包括User类页面,因此应该是:

// DB Class
include_once('php/classes/DB_CONFIG.php');
// User class
include_once('php/classes/class.user.php');
// $con is your DB class, and `Fetch` is in that class
$all    =   $con->Fetch("select * from users");

您忘记包含数据库类:

将此添加到脚本顶部:

include_once('path/to/DB_CONFIG.php');

$all=$user1->Fetch("从用户中选择*");

将其更改为:

$all    =   $con->Fetch("select * from users");
            //here con is construct parameter not an object

到此:

$all    =   $user1->Fetch("select * from users");