Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
2  
What isn't working? –  John Conde Jan 22 '13 at 15:56
1  
Aside from using the deprecated mysql_* functions I can't see anything odd in a quick look. What doesn't happen that should? –  lc. Jan 22 '13 at 15:58
    
why are you running two queries? this should be done as a single JOINed query. –  Marc B Jan 22 '13 at 15:59
    
What happens when you run this? What is returned - are you just getting an empty array? –  Arthur Richards Jan 22 '13 at 16:05

3 Answers 3

up vote 0 down vote accepted
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.

share|improve this answer
    
You don't need to include a field name since $i is just an associative array. But you are correct that 'k' was missing a $ sign - that is likely what's causing the problem. I totally missed that when reading his code! –  Arthur Richards Jan 22 '13 at 17:19
$_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.

share|improve this answer
    
Are you sure you mean $i++? –  Sammitch Jan 22 '13 at 16:20
    
haha, no. thanks @Sammitch - I shouldn't do this stuff before I drink my coffee :p –  Arthur Richards Jan 22 '13 at 16:27
  1. Turn up error_reporting to see if your code is generating errors.
  2. Check if your query is successful if( ! $r=mysql_query($q) ) { die(mysql_error()); }
  3. var_dump($_subjects); to see if the data is what you expect it to be.
  4. Maybe actually tell us what's going wrong? You've just posted a block of code and told us "it doesn't work." which isn't terribly indicative of a problem.
  5. $k is irrelevant, just use $_subjects[]=$i;. [wouldn't cause an error, just easier]
  6. Stop using mysql_* functions and port your code to PDO or MySQLi to:
    • Benefit from parameterized queries which can help protect against SQL injection.
    • Stop everyone on SO starting an unrelated argument in the comments about it.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.