1

I'm trying to retrieve specific table data from MySQL and using PHP to display the information. Should be simple enough to retrieve the data and print it but I get undefined index only when retrieving the data and trying to display.

I'm retrieving the information and displaying the information in a profile page according to the username in the header.

if(isset($_GET['username']))
{
    $username           = $_GET['username'];
    $profile_uid        = $Wall->useriD($username);
    $profile_details    = $Wall->userDetails($profile_uid);
    $friend_count       = $profile_details['friendcount']; //issue here
} else {
    header('Location:404.php');
}

I get the undefined index on the $friend_count = $profile_details['friendcount']; but I'm not sure why, I believed everything checked correctly but apparently not. Here is how I'm fetching the information.

public function useriD($username)
{
    $sth = $this->db->prepare("SELECT uiD FROM users WHERE username = :username AND status='1'");
    $sth->execute(array(':username' => $username));

    if($sth->rowCount() == 1)
    {
        $row = $sth->fetch();
        return $row['uiD'];
    } else {
        return false;
    }
}

public function userDetails($uiD)
{
    $sth = $this->db->prepare("SELECT * FROM users WHERE uiD = :uiD AND status='1'");
    $sth->execute(array(':uiD' => $uiD));

    $data = $sth->fetchAll();
    return $data;
}

The issue is when I try to display the information such , I've been at it for a few hours and I'm stuck. Any leads would be great.

3
  • 4
    do a var_dump($profile_details). you'll see exactly what's available in that array. Note that fetchAll() will return an array-of-arrays, not just one data row as an array. Commented Sep 5, 2013 at 18:15
  • 2
    Yeah what Marc B says, so its most likely $profile_details[0]['friendcount'], but do check. Commented Sep 5, 2013 at 18:16
  • You guys are correct. Thank you for the support. I've always wondered what the [0] stands for, is it the current id that's loaded into the header? Commented Sep 5, 2013 at 18:17

2 Answers 2

1

Since you're only interested in one row, use fetch() instead of fetchAll():

$data = $sth->fetch();
Sign up to request clarification or add additional context in comments.

Comments

0

In your userDetails function, fix the last statement:

return $data[0];

(You might also want to precheck whether $data[0] exists, and if it does not, return NULL, FALSE or throw an exception.)

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.