0

i know this has been posted many many times but ive been searching everything and looking over old posts but i still cannot figure this out and why it keeps reading it as non object heres my code

<?php
require 'dbconfig.php';
class USER
{
    private $db;

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

    public function register($fname,$lname,$uname,$umail,$upass,$utitle)
    {
        try
        {
            $new_password = MD5($upass);

            $stmt = $this->db->prepare("INSERT INTO users(user_name,user_email,user_pass,user_title) 
                                                       VALUES(:uname, :umail, :upass, :utitle)");

            $stmt->bindparam(":uname", $uname);
            $stmt->bindparam(":umail", $umail);
            $stmt->bindparam(":upass", $new_password);  
            $stmt->bindparam(":utitle", $utitle);           

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }

    public function login($uname,$umail,$upass)
    {
        try
        {
            $stmt = $this->db->prepare("SELECT * FROM users WHERE user_name=:uname OR user_email=:umail LIMIT 1");
            $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() > 0)
            {
                if($userRow['user_pass']==MD5($upass))
                {
                    $_SESSION['user_session'] = $userRow['user_id'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    public function is_loggedin()
    {
        if(isset($_SESSION['user_session']))
        {
            return true;
        }
    }

    public function redirect($url)
    {
        header("Location: $url");
    }

    public function logout()
    {
        session_destroy();
        unset($_SESSION['user_session']);
        return true;
    }
}

/* SHOUTBOX FUNCTIONS */
function getShouts( $limit )
    {
        global $DB_con, $db_table_prefix; 
        $stmt = $DB_con->prepare("SELECT userid, shout FROM shouts WHERE userid=:userid AND shout=:shout id LIMIT ?");
        $stmt->bind_param("i", $limit);
        $stmt->execute(array(':userid'=>$userid, ':shout'=>$shout));
        $stmt->bind_result($userid, $shout);
        $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
        $stmt->close();
        return ($userRow);
    }

    function sendShout( $uid, $shout ){
        global $DB_con, $db_table_prefix;
        $uid = html_sanitize($uid);
        $shout = html_sanitize($shout);

        if($shout == "/prune")
        {
            $mysqli->query("TRUNCATE TABLE shouts");
        }

        $stmt = $DB_con->prepare("INSERT INTO shouts
        (userid,shout)
        VALUES (?,?)");
        $stmt->bind_param("is", $uid, $shout);
        $stmt->execute();
        $stmt->close();
    }

?>

my error is reading on line #87 i cant see what im doing wrong here

Edited by Kevin_22

3
Contributors
4
Replies
19
Views
1 Year
Discussion Span
Last Post by Kevin_22
0

In your constructor you're setting $this->db to be equal to $DB_con, and using $this->db in your login method.
So two questions: is $DN_con a valid connection when you pass it through to the class? And does the login method work?

if the login method works use $this->db and tour problem is solved. Otherwise make sure $DB_con is an open and valid connection and make sure it is available to the getShouts method.

0

yes the login and registration method works just fine but when i do $this->db i get
Fatal error: Using $this when not in object context in

now im new to PDO so im trying to learn but everything i try does not work

0

It's logic to not work bcoz u are using a global and another global for what god knows.

/* SHOUTBOX FUNCTIONS */
function getShouts( $limit )
    {

    try
        {

        $stmt = $this->db->prepare("SELECT userid, shout FROM shouts WHERE userid=:userid AND shout=:shout id LIMIT ?");

        $stmt->bind_param("i", $limit);

        $stmt->execute(array(':userid'=>$userid, ':shout'=>$shout));

        $stmt->bind_result($userid, $shout);

        $userRow = $stmt->fetch(PDO::FETCH_ASSOC);

        $stmt->close();

        return ($userRow);

    }
        catch(PDOException $e)

        {
            echo $e->getMessage();
        }

}


function sendShout( $uid, $shout ){
       try
        {

        $uid = html_sanitize($uid);

        $shout = html_sanitize($shout);

        if($shout == "/prune")
        {
            $mysqli->query("TRUNCATE TABLE shouts");
        }

        $stmt = $this->db->prepare("INSERT INTO shouts
        (userid,shout)
        VALUES (?,?)");

        $stmt->bind_param("is", $uid, $shout);

        $stmt->execute();

        $stmt->close();
    }
        catch(PDOException $e)

        {
            echo $e->getMessage();
        }

}

Edited by quadmachine

0

i wasnt completly sure either thats how i did it when i was using mysqli but i have updated the coding with how you did but now the error goes to
Fatal error: Using $this when not in object contex i keep trying to make it a object but nothing is working

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.