0
<?php
require_once (realpath(dirname(__FILE__) . '/../includes/database.php'));

class User {
    public $email;
    public $password;

    public function find_email($email, $password) {
        global $database;
        $pswd = substr(md5($password), 0, 25);
        $results_array = self::find_by_sql("SELECT * FROM tbl_users where email_id='".$email."' AND password='".$pswd."'");
        return !empty($results_array)? array_shift($results_array) : false;
   }

    public static function find_by_sql($sql){
        global $database;
        $results = $database -> query($sql);
        $object_array = array();
        while($row = $database -> fetch_array($results)){
            $object_array[] = self::instantiate($row);
       }
        return $object_array;
    }

    public static function instantiate($row) {
         $event   =   new self;
         foreach($row as $attribute => $value) {
             if($event -> has_attribute($attribute)) {
                 $event -> $attribute = $value;
             }
         }
         return $event;
    }
    private function has_attribute($attribute) {
        $object_vars = get_object_vars($this);
        return array_key_exists($attribute, $object_vars);
    }
}

if (isset($_GET['email']) && isset($_GET['password'])) {
    $result = new User();
    $result->find_email($_GET['email'], $_GET['password']);
    echo json_encode($result);
}
?>

This is the login.php which is supposed to print out the json for the required user, but whenever I try to get the json, this is getting returned.

{"email":null,"password":null}

Any help would be appreciated. Thanks.

7
  • 1
    Hi, this calls for basic debugging first. At which point does what go wrong? Can you do test outputs to find out at which point the values become empty? Commented Jul 4, 2013 at 7:48
  • Where exactly are those values supposed to come from? Commented Jul 4, 2013 at 7:48
  • Please print_r($_GET), also, email_id, is that a number or a string? Commented Jul 4, 2013 at 7:49
  • first check $result data is it containing values,nothing to do with json_encode() it simply return json_object Commented Jul 4, 2013 at 7:50
  • @IgnacioVazquez-Abrams I've included a line on top require_once... that connects with the database. Commented Jul 4, 2013 at 7:51

1 Answer 1

1

You don't do anything with the result of find_email. Your class doesn't update it's own properties when find_email is called. Instead, it returns a new instance of the class with the email and password properties set, so you need to capture the return value and encode that.

Change to:

$result = new User();
$user = $result->find_email($_GET['email'], $_GET['password']);
echo json_encode($user);

Side note: have a look at SOLID and Dependency Injection. DI would be preferred over having a global $Database.

Sign up to request clarification or add additional context in comments.

4 Comments

Better yet, refactor so you don't have to create the temporary User object just to call find_email.
I agree there is a lot of room for refactoring here, the class is doing a bit too much. It seems the class is trying to be both a user entity and a user manager/factory.
This seems to fix the issue. Thanks a lot. Was working on some project, trying out php for the first time. This is a part of feeder btw, for an iOS app. Will look into those SOLID and Dependency Injection. Thanks again.
No problem. SOLID and DI can get quite complex so don't let it overwhelm you. If you just understand the basics/reasoning behind them, it will help a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.