意外的T_CATCH,期待T_FUNCTION

时间:2010-08-28 17:06:36

标签: php error-handling

今天,在尝试编写此User类时,我在下面突出显示的行中遇到了上述错误,并且想知道你很多想到它。

class User {
  public $username;
  public $password;
  public $user_info;
  public $connection;

  function __construct($username) {
   $connection = new Database();
   try {
   $user_info = $connection->query("SELECT * FROM `users` WHERE `username` = '$username'");
   if ($user_info == null) {
    throw new Exception("Could not find user details, please try again later!");
   }
   ***} catch (Exception $login) {***
    die($login->getMessage());
   }
   session_start();
   $_SESSION["dgs_tech"] = $user_info;
  }

  function update_info() {
   $user_info = $connection->query("SELECT * FROM `users` WHERE `username` = '$username'");
   if ($user_info == null) {
    throw new Exception("Could not find user details, please try again later!");
   }
   } catch (Exception $login) {
    die($login->getMessage());
   }
   $_SESSION{"dgs_tech"] = $user_info;
  }

  function return_info() {
   $text = "<p>"
   foreach($user_info as $name => $item) {
    $text += "<h1>$name:</h1>$item<br/>";
   }
   $text += "</p>";
   return $text;
  }
 }

3 个答案:

答案 0 :(得分:1)

我认为这个错误即将发挥作用update_info() 你没有“尝试{”那里

答案 1 :(得分:1)

更正后的代码:立即试用:

class User
{
    public $username;
    public $password;
    public $user_info;
    public $connection;

    function __construct($username)
    {
        $connection = new Database();
        try
        {
            $user_info = $connection->query("SELECT * FROM `users` WHERE `username` = '$username'");
            if ($user_info == null)
            {
                throw new Exception("Could not find user details, please try again later!");
            }
        }
        catch (Exception $login)
        {
            die($login->getMessage());
        }
        session_start();
        $_SESSION["dgs_tech"] = $user_info;
    }

    function update_info()
    {
        try
        {
            $user_info = $connection->query("SELECT * FROM `users` WHERE `username` = '$username'");
            if ($user_info == null)
            {
                throw new Exception("Could not find user details, please try again later!");
            }
        }
        catch (Exception $login)
        {
            die($login->getMessage());
        }
        $_SESSION["dgs_tech"] = $user_info;
    }

    function return_info()
    {
        $text = "<p>"
        foreach($user_info as $name => $item)
        {
            $text += "<h1>$name:</h1>$item<br/>";
        }
        $text += "</p>";
        return $text;
    }
}

它有一个问题,从一个函数开始尝试,另一个函数结束。

你应该正确缩进你的代码。错误将自动自动

答案 2 :(得分:0)

您的构造函数accolades {}未正确关闭,并且您在函数外部有代码。