0

Can anyone tell me how to display the value of next from the code below?

In my user.php file i have next content:

class User {
    protected $userID;
    protected $useremail;
    protected $userPassword;
    public function __construct() {
        $this->userID = preg_replace('#[^0-9]#i', '', 
            $_SESSION['user_id']);
        $this->useremail = preg_replace('#[^A-Za-z0-9@_.-]#i', '', 
            $_SESSION['user']);
        $this->userPassword = preg_replace('#[^A-Za-z0-9]#i', '', 
            $_SESSION['user_password']);
    }

public function UserInfoQuery() {
    $sql = "SELECT * FROM users WHERE id = 
       '$this->userID' AND email = '$this->useremail' AND 
        password = '$this->userPassword' LIMIT 1";
    $res = mysql_query($sql) or die(mysql_error()); 
    $userMatch = mysql_numrows($res);

    if ($userMatch == 1) {
        while($row = mysql_fetch_array($res)) {
            $userData = array(
                $userFirstname = $row['firstName'],
                $userLastname = $row['lastName'],
                $userBirthdate = $row['birthDate'],
                $userSex = $row['sex'],
                $userEmail = $row['email'],
                $userCountry = $row['country'],
                $userRegion = $row['region']);
            }
        }

        return $userData;
    }
}

In my index php file when I try:

$User = new User();
print_r($User->UserInfoQuery());

I have next results:

Array ( [0] => firstname [1] => 
    lastname [2] => 
    1990-11-23 [3] => 
    male [4] => 
    mail [5] => 
    Srbija [6] => town ) 

How I can echo just the first and last names?

0

2 Answers 2

3

This:

array($userFirstname = $row['firstName'])

assigns the value of $row['firstName'] to the variable $userFirstname, then puts the result of the assignment (the value of $row['firstName']) into an array. It's the same as writing:

$userFirstname = $row['firstName'];
array($row['firstName']);

To declare an array with the key userFirstname, you need to write:

array('userFirstname' => $row['firstName'])

From here, you have a normal array you can access:

$userinfo = $User->UserInfoQuery();
echo $userinfo['userFirstname'];

This does seem somewhat clunky though, and honestly, you're not using objects very well here. You should save the data queried from the database into properties of the object, then use getters to access those properties one by one or all together. How to design a proper object is a little beyond the scope/point of this answer though.

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

3 Comments

I do that but how to display, i try echo $User->UserInfoQuery('userFirstname'); But my result is "Array"
@Stefan Then you need to write your method so it returns only the username when you call it like $User->UserInfoQuery('userFirstname'). See the update to my answer though. Your object should not query the database every single time you call UserInfoQuery().
Hmmm yep i have too much querys :( Thanks
0

You should have your array the following way:

$userData = array(
            'Firstname' = $row['firstName'],
            'lastname = $row['lastName'],
            'birthdate = $row['birthDate'],
            'sex = $row['sex'],
            'email = $row['email'],
            'country = $row['country'],
            'region = $row['region']
       );
}

1 Comment

I do that but how to display, i try echo $User->UserInfoQuery('userFirstname'); But my result is "Array"

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.