Reputation: 89
I have problem undefined variable. The thing is, I found a tutorial to make a type micro blog Twitter, had some problems, I decided, but this is taking me patience.
Error Message:
Notice: Undefined variable: extra in caminho\do\arquivo\functions.php on line 58
function:
function show_users($user_id=0){ if ($user_id > 0){ $follow = array(); $fsql = "SELECT user_id FROM following WHERE follower_id='$user_id'"; $fresult = mysql_query($fsql); while($f = mysql_fetch_object($fresult)){ //array_push($follow, $f->user_id); $follow[] = $f->user_id; } if (count($follow)){ $id_string = implode(',', $follow); $extra = " AND id IN ($id_string)"; }else{ return array(); } } $users = array(); $sql = "SELECT id, username FROM users WHERE status='active' $extra ORDER BY username"; $result = mysql_query($sql); while ($data = mysql_fetch_object($result)){ $users[$data->id] = $data->username; } return $users; }
The line in question is:
$sql = "SELECT id, username FROM users WHERE status='active' $extra ORDER BY username";
Any suggestions?
Upvotes: 0
Views: 119
Reputation: 21
function show_users($user_id=0,extra='') // use this
{
if ($user_id > 0){
$follow = array();
$fsql = "SELECT user_id FROM following WHERE follower_id='$user_id'";
$fresult = mysql_query($fsql);
while($f = mysql_fetch_object($fresult)){
//array_push($follow, $f->user_id);
$follow[] = $f->user_id;
}
if (count($follow)){
$id_string = implode(',', $follow);
$extra = " AND id IN ($id_string)";
}else{
return array();
}
}
Upvotes: 0
Reputation: 1063
There are two ways :
1. function show_users($user_id=0){
$extra='';
2. function show_users($user_id=0,$extra=''){
Actually you are declaring $extra only when you have $user_id > 0 BUT you must be having $user_id equals to or less than 0 for the current situation and thus your query couldn't find $extra.
I hope you get an idea.
Upvotes: 1
Reputation: 360592
function show_users(...) {
$extra = ''; // <--just add this line
blah blah blah blah blah
}
In your code, $extra gets defined ONLY if both if()
tests succeed. Obviously they're not, so you end up with an undefined $extra in your sql string.
Upvotes: 0
Reputation: 2970
function show_users($user_id=0){
$extra = ''; //if no extra, you'll get just an empty string
//etc...
}
Upvotes: 0