我试图在 MVC 的模型中使用 $_session 变量来获取登录用户的分数和名称,然后通过数组将其发送到视图。
这是我的游戏模型。我将如何从视图中获取 SESSION 变量?当用户提交登录表单时,页面转到 login.php,它检查用户是否存在,如果存在则启动会话: $_SESSION['username']=$_POST["username"]; 如果我在 mySQL 查询中将用户名设置为“test”,它就可以工作。
public function game()
{
include 'connect.php';
//$query = "SELECT * FROM users WHERE USERNAME='"$this->$_SESSION["username"]."'";
$query = "SELECT * FROM users WHERE USERNAME='test'";
$result = mysql_query($query) or die (mysql_error());
$score = mysql_fetch_array($result) or die(mysql_error());
$user = $score['username'];
$s1 = $score['score1'];
$s2 = $score['score2'];
$s3 = $score['score3'];
//store values to array for view to see, extract() array so views can use
them as variables
$game_model = array('user' => $user, 's1' => $s1, 's2' => $s2, 's3' => $s3);
return game $model;
}