1

I'm trying to enter all the course_code that have a score < 40 into array and echo it out somewhere. The print_r($carry_over) returns nothing!

$carry_over = array(); 

while ($row8 = mysql_fetch_assoc($query8)) {

   if ($row8['score'] < 40) {
        $carry_over[] = array('m.course_code' =>$row8['course_code']);
   } 
}

print_r($carry_over);

$query8 = mysql_query("SELECT  m.score , m.course_code FROM maintable AS m  
                       INNER JOIN students AS s ON m.matric_no = s.matric_no
                       INNER JOIN courses AS c ON m.course_code = c.course_code
                       WHERE m.matric_no = '".$matric_no."'
                       AND m.level = '".$level."'
                       AND m.score < 40"
                      ) or die (mysql_error());
1
  • query8 should return the courses with scores less than 40 so why the if test? Commented Mar 17, 2012 at 4:57

3 Answers 3

2

Your $query8 variable (the database query) should be defined before the while() function. Right now, the while() function iterates over 0 rows which, of course, results in an empty array.

$carry_over = array(); 

while ($row8 = mysql_fetch_array($query8))
{
    $carry_over[] = $row8['course_code'];
}

Since you already check for rows where the score is less than 40 in your SELECT query, the check inside the while() function is redundant. You also missed a single-quote in front of course_code (it was previously a dot), and finally; adding an array inside the $carry_over array is unnecessary when you can just add the value directly to the first array.

2nd UPDATE

$matric_no = MAKE_SURE_TO_DEFINE_THIS;
$level = MAKE_SURE_TO_DEFINE_THIS;

// Fetch rows
$query8 = mysql_query("SELECT maintable.score, maintable.course_code, maintable.matric_no, maintable.level, students.matric_no, courses.course_code FROM maintable, students, courses WHERE (maintable.matric_no = '" . $matric_no . "' AND maintable.matric_no = students.matric_no) AND (maintable.course_code = courses.course_code) AND (maintable.level = '" . $level . "')");

$carry_over = array();

while ($row8 = mysql_fetch_array($query8))
{
    // Save data to array
    $carry_over[] = $row8['course_code'];
}

echo 'We found ' . mysql_num_rows($query8) . ' rows';

print_r($carry_over); // DEBUG
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help. But i'm still getting the same result, after implementing the errors you spotted. Thanks
@dotunoyesanmi - after moving the $query8 to before the while() function, try replacing your code with the one I just posted above
Thank you very much for the UPDATE. I updated with your code and also added print_r($carry_over); it returne...... Array(). Does that mean the query is empty? But when i used $query8 in mysql_num_rows() i got 2 and that is what i expected. Where could the error be. Thanks
@dotunoyesanmi - Try the code I just posted in my 2nd update! The echo should verify that you get the 2 rows you're expecting and the print_r() should def work... tell me if you run into any problems
Thanks Tom, the second update is working well now. Cant thank you enough. I'll mark your answer now.
1

The array loop should be If($row8['score'] < 40) $carry_over[] = $row8['course_code'];

5 Comments

Check your query whether score < 40 then debug in the if statement by echo sth...if nothing means the query has no score < 40....my syntax definitely works if there is score < 40
i've done a mysql_num_rows($query8) and it returned 2, and that is correct.
I think your query too complex.Try Tom's query below which you do not need INNER join...
Just one more thing check your score value type.Is it integer?
@HieuVanMach - had missed a few things in my query, was about to change it but removed it since he stated that he actually got 2 rows in return from his, which proves that it's working
0

I think you might have to debug $row8 array first. to see what is inside or it probably got nothing at all

3 Comments

var_dump($row8) gave boolean false
Yes, that's answer everything. You got no result an the beginning.
i've done a mysql_num_rows ($query8) and it returned 2, and that is correct. –

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.