0

I am creating a user "friend system" from a tutorial on Youtube. Everything is working fine except the "members" page. I want it to display all the site's members by their username. I have been over this part of the tutorial several times, and am pretty sure that i have not made an error. But, when I test the page out, it lists all the members, but does not display the username. I get this error

"Notice: Undefined index: $field in J:\TestSite\socialnetworkcode\functions.php on line 16"

I know that the variable "$field" is undefined in the functions page, the strange part is that his (the person who created the tutorial) page works fine and diplays the usernames. I do not know what to define it as, or why his works without defining "$field" and mine doesn't.

<?php

session_start();

function loggedin(){
    if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){
   return true;
} else {
    return false;
}
}

function getuser($id, $field){
    $query = mysql_query("SELECT $field FROM users WHERE id='$id'");
    $run = mysql_fetch_array($query);
    return $run['$field'];

}

?>
  • danron, look closer. He's trying to return the dynamic variable $field, not the string $field. Your solution wouldn't work! – bpeterson76 Apr 21 '15 at 18:40

1 Answers1

1

You're telling it to return the string $field, when you really want to have it return the variable $field.

Change your last line to return $run[$field]

bpeterson76
  • 12,918
  • 5
  • 49
  • 82