views:

17

answers:

2

Today, while trying to write this User class, I ran into the above error on the highlighted line below, and was wondering what you lot thought of it.

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  A: 

I think this error is about to function update_info() You do not have "try{" there

canni
Unfortunately, pretty much as soon as I posted this, I noticed the lack of "try {". There were several other errors which I fixed, but thanks very much anyway :)
Jono
+1  A: 

Corrected code: Try it now:

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

It had the problem where Try started from one function and ending on another.

And you should correctly indent your code. The bugs will become clear automagically.

shamittomar
Why You're doing this thing for him? :P We should post answers for questions witch will allow to find and fix, not ready-to-go "I did it for You" ;)
canni
@Dariusz, He can not correct it himself, that's why he asked it here. I think we can help our fellow coders by being clear and elaborate.
shamittomar
Yep, but that way only teach method: "I can't do it, let others do it for me", and pointing where-to-find, forces user, to teach where he have made mistake... Do You want everyone only ask heare "fix it for me please?" ;)
canni
@Dariusz, I corrected the code, along with specifying the reason and giving tip on correct indentation which will let him not repeat the mistake. What else do you want from me ?
shamittomar
@shamittomar I do not want anything, there is nothin' wrong in Yours answer, i just was only surprised, that You have pasted ready to go code instead of just (only) pointing out mistake :) not more not less
canni
@Dariusz, Hmmm..... it's just my habit to post working codes :)
shamittomar
Sounds like Dariusz is worried that Shamittomar might win the "best answer"... problem is, this question is asked by a new person that will probably never get any response back. Hope I'm wrong.
cdburgess
@cdburgess every kid has to get through it, ofcourse ;) :P
canni