Reputation: 107
What is wrong with this code? I am trying to return an array and use it in the caller function.
function my_subjects()
{
$all_my_teams=my_teams();
$k=0;
$_subjects=array();
while($team=mysql_fetch_assoc($all_my_teams))
{
$q="SELECT $this->user_field FROM $team";
$r=mysql_query($q);
while($i=mysql_fetch_assoc($r))
{
$_subjects[k++]=$i;
}
}
return $_subjects;
}
Note: the function my_teams() returns the value similar to the $r variable which is used through all_my_teams variable. it contains all the names of teams.
Upvotes: 0
Views: 86
Reputation: 491
$_subjects[$k++] = $i;
should be fine, since you're using mysql_fetch_assoc() $i
will contain an associative array of the result set.
If this is returning an empty array (is that the correct problem?), you should double check that your sql is correct and actually returning data/data that you expect.
Edit: Like Hanky Panky mentioned, 'k' is missing a $ sign in your code - that is likely to be your problem - PHP should be throwing a parse error for that one, so make sure you have error_reporting() enabled.
Upvotes: 0
Reputation: 32232
error_reporting
to see if your code is generating errors.if( ! $r=mysql_query($q) ) { die(mysql_error()); }
var_dump($_subjects);
to see if the data is what you expect it to be.$k
is irrelevant, just use $_subjects[]=$i;
. [wouldn't cause an error, just easier]Upvotes: 1
Reputation: 46900
while($i=mysql_fetch_assoc($r))
{
$_subjects[k++]=$i;
}
Here you also have to provide a field name for $i
. Something like
while($i=mysql_fetch_assoc($r))
{
$_subjects[$k++]=$i["field_name"];
}
Array returning part is just fine.
Edit: Your variable k was missing a $ sign as well.
Upvotes: 0