2010-08-28 49 views
0

今天,在试图写这个User类的时候,我在下面突出显示的行中遇到了上面的错误,并且想知道你对它的想法。意想不到的T_CATCH,期待T_FUNCTION

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; 
    } 
} 

回答

1

我觉得这个误差约为运作update_info() 你不必 “尝试{” 有

+0

不幸的是,当我发布这些内容时,我几乎注意到缺少“try {”。还有其他几个我修复的错误,但非常感谢:) – Jono 2010-08-28 19:15:35

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; 
    } 
} 

它有问题在哪里尝试从一个功能开始,结束于另一个功能。

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

+0

为什么你为他做这件事? :P我们应该回答问题女巫将允许找到并修复,而不是准备好“我为你做了”;) – canni 2010-08-28 17:13:45

+1

@Dariusz,他不能自己纠正它,这就是他为什么在这里问它。我认为我们可以通过清晰和详细的方式帮助我们的同行编码人员。 – shamittomar 2010-08-28 17:15:47

+0

是的,但这种方式只教方法:“我做不到,让别人为我做”,并指出在哪里寻找,迫使用户,教他错误的地方......你想要吗?大家只问heare“请帮我修好?” ;) – canni 2010-08-28 17:18:24

0

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