Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7005207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:19:19+00:00 2026-05-27T21:19:19+00:00

I’m working on a forum software currently. I have a class called User, within

  • 0

I’m working on a forum software currently.

I have a class called User, within that class I have a method called GetUserGroup to determine what group the user is in.

I am running the query and assoc the same way I’ve been doing with all my other queries, I’m not sure what I’m doing wrong, I’ve looked over the query for syntax errors but just don’t see any.

Catchable fatal error: Object of class User could not be converted to string in C:\xampp\htdocs\forums\index.php on line 22

Here’s the whole page:

<?php 
include_once('connect.php');
session_start();

if (isset($_SESSION['username'])) {
    $username = $_SESSION['username'];

}

class User {

    public $usergroup;
    public $user;

    function __construct() {
        if (isset($_SESSION['username'])) {
            $this->user = $_SESSION['username'];
        }
    }

    public function GetUserGroup() {
        $find_group = "SELECT group FROM users WHERE username='$this->user'";
        $run_find_group = mysql_query($find_group);
        $find_group_assoc = mysql_fetch_assoc($run_find_group);
        $this->usergroup = $find_group_assoc['group'];
    }
}

class Forum {

    function __construct() {

    }

    public function DisplayForums() {

        $find = "SELECT id,name,description FROM forums";
        $run_find = mysql_query($find);

        while ($is = mysql_fetch_assoc($run_find)) {
            $forum_id = $is['id'];
            $forum_name = $is['name'];
            $forum_description = $is['description'];

            echo "<div  style='background:#FF6699;width:1000px;'>";
            echo "Forum: <a href='topics.php?t='$forum_id'>".$forum_name."</a><br/>".$forum_description."<br/><hr>";
            echo "</div>";
        }
    }
}

$forum = new Forum();
if (isset($_SESSION['username'])) {
    $_SESSION['username'] = new User();
    $_SESSION['username']->GetUserGroup();
}
?>
<html>
<head>
    <title>Home</title>
</head>
<body>
<?php 
if (isset($_SESSION['username'])) {
    echo "Welcome, " . $username . "!";
    if ($_SESSION['username']->usergroup==admin) {
        echo "<span align='right'><a href='/admin/index.php'>Admin CP</a></span>";
    }
    $forum->DisplayForums(); 
} else {
    $forum->DisplayForums(); 
    echo "
    <form action='login.php' method='post'>
    <table>
        <tr>
            <td>Username: </td>
            <td><input type='text' name='username' /></td>
        </tr>
        <tr>
            <td>Password: </td>
            <td><input type='text' name='password' /></td>
        </tr>
        <tr>
            <td><input type='submit' name='login_submit' value='Login' /></td>
        </tr>
    </table>
    </form>";
}
?>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:19:20+00:00Added an answer on May 27, 2026 at 9:19 pm

    Your problem is here:

    $_SESSION['username'] = new User();
    

    You’re assigning a User object to the $_SESSION['username']. So, when $_SESSION['username']->GetUserGroup() is called $this->user is a User object.

    You need to either set $_SESSION['username'] to the username, and not an object. Or make a method to get the username from the object. You should add a getUsername method or something to User.

    public function GetUserGroup() {
        $user = $this->user->getUsername();
        $find_group = "SELECT group FROM users WHERE username='$user'";
        $run_find_group = mysql_query($find_group);
        $find_group_assoc = mysql_fetch_assoc($run_find_group);
        $this->usergroup = $find_group_assoc['group'];
    }
    

    You can also use PHP’s __toString method.

    function __toString(){
      return $this->getUsername();
    }
    

    If you use __toString, then:

    $find_group = "SELECT group FROM users WHERE username='$this->user'";
    

    will work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.