Uncaught Error: Call to a member function buildUserData()

AlexJBallz Source

Okay so heres the issue, I am trying to making a fancy login system that stores the users data in a php class so it is easy to access. However when I try to call the function that will build the user data it throws this exception.

Fatal error: Uncaught Error: Call to a member function buildUserData() on string in C:\xampp\htdocs\cfgs\submit\login.php:38 Stack trace: #0 {main} thrown in C:\xampp\htdocs\cfgs\submit\login.php on line 38

Here is the userData class code

<?php

    class userData {

        public $accId = null;
        public $username = null;
        public $rank = null;
        public $vip_rank = null;
        public $email = null;
        public $auth = null;

        function buildUserData($id) {
            $result = $db->query("SELECT * FROM users WHERE id = '$id'");
            while ($row = $result->fetch_assoc()) {
                $this->accId = $id;
                $this->username = $row['username'];
                $this->rank = $row['rank'];
                $this->vip_rank = $row['rank_vip'];
                $this->email = $row['mail'];
                $this->auth = $row['auth'];
            }

        }

    }

?>

This is the login.php code

<?php

    require_once '../../global.php';

    if(!isset($_POST['submit'])) {
        header("Location: ../../index");
        return;
    } else {

        $user = $db->escapestring($_POST['user']);
        $pass = $db->escapestring($_POST['pass']);

        if (empty($user) || empty($pass)) {
            $_SESSION['logError'] = "Both fields must be filled!";
            header("Location: ../../index");
            return;
        } else {
            $result = $db->query("SELECT * FROM users WHERE username = '$user'");
            $result = $db->getrows($result);
            if ($result < 1) {
                $_SESSION['logError'] = "Username does not exist!";
                header("Location: ../../index");
                return;
            } else {
                $pass = md5($pass);
                $result = $db->query("SELECT * FROM users WHERE username = '$user' AND password = '$pass'");
                $result = $db->getrows($result);
                if ($result < 1) {
                    $_SESSION['logError'] = "Details do not match!";
                    header("Location: ../../index");
                    return;
                } else {
                    $result = $db->query("SELECT * FROM users WHERE username = '$user' AND password = '$pass'");
                    while($row = $result->fetch_assoc()){
                        $username = $row['username'];
                        $id = $row['id'];
                    } 
                    $user->buildUserData($id);
                    $_SESSION['logError'] = "Hello ". $user->username;
                    header("Location: ../../index");
                    return;
                }
            }
        }
    }

?>

This is the global.php code

    <?php

        session_start();

        require_once 'cfgs/class.database.php';
        require_once 'cfgs/class.user.php';

        $user = new userData; // I don't want to build data just yet

        $db = new database;
        $db->conn = $db->connect();


    ?>

And finally my database handler

<?php

    class database {

        public $host = "_";
        public $user = "_";
        public $pass = "_";
        public $db = "_";

        public $conn = null;

        function connect() {
            return mysqli_connect($this->host, $this->user, $this->pass, $this->db);
        }

        function query($sql) {
            return mysqli_query($this->conn, $sql);
        }

        function escapestring($string){
            return mysqli_real_escape_string($this->conn, $string);
        }

        function getrows($sql){
            return mysqli_num_rows($sql);
        }
    }

?>

I did search for a solution myself but it turns out to be one of those specific things that is difficult to find the answer you're looking for.

phpmysqli

Answers

answered 4 months ago user9487972 #1

while you initialise $user just fine:

$user = new userData;

you later overwrite the variable:

$user = $db->escapestring($_POST['user']);

one of the 2 needs a new name,

comments powered by Disqus