我目前正在开发一个论坛软件。
我有一个名为User的类,其中包含一个名为GetUserGroup的方法,用于确定用户所在的组。
我正在使用与所有其他查询相同的方式运行查询和关联,但我不确定我做错了什么,我已经检查了语法错误,但没有看到任何问题。
可捕获的致命错误:在C:\xampp\htdocs\forums\index.php的第22行中,无法将User类的对象转换为字符串。
以下是整个页面:
<?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>