0

I have the following function:

function getUser($user_id){

    $mysqli = dbConnect();

    $gu = "select * from users where user_id = '$user_id'";
    $ru = $mysqli->query($gu);

    $user = $ru->fetch_array();

    return $user;
}  

Which is called eg:

$user_id =  $_SESSION[user_id];

getUser($user_id);

Then I want to simply echo fields i want, e.g. name. But, when I try the following, it returns empty

echo "users name is $user['name']"; // returns: users name is

Is there a better way to do this?

UPDATE Also tried the following but still empty:

function getUser($user_id){

    $mysqli = dbConnect();

    $gu = "select * from users where user_id = '$user_id'";
    $ru = $mysqli->query($gu);

    $user = array();
    while($row = $ru->fetch_array()) {
        $user[] = $row;
    }

    return $user;
}
2
  • 1
    You doesn't store the result of getUser(). Check the answer of @CodeBird Commented May 12, 2013 at 20:14
  • The comments/answers already posted are correct. A couple of other things: 1) What is the type of the user_id column? You're querying it like it's a string type. 2) Have you tried checking into the return value of your fetch_array() call? var_dump() or print_r() if you've no debugger handy. Commented May 12, 2013 at 20:19

2 Answers 2

2

your line:

getUser($user_id);

should be:

$user=getUser($user_id);

This way you'll be setting $user to the array the getUser returns, then you can use it.

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

Comments

0

Remove the single quotes when printing an array, you echo may needed to be like:

echo "users name is $user[name]";

Comments

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.