0

I have this bit of code that doesn't produce anything, not even an error message. I'm trying to echo the result inside the while loop, but even that doesn't show anything. Any tips?

foreach($droppedStudentIds as $value){

        $query3 = "select * from student_classlists where StudentId = '$value' and ClassListDate = (select max(ClassListDate) from student_classlists)";    

        if($result = mysqli_query($mysqli, $query3)) {

            while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

                echo "Date: ".$row['ClassListDate'];

                $droppedStudentIds[$value][] = $row['ClassListDate'];

            }

        mysqli_free_result($result);

        } else die ("Could not execute query 3");

}

My goal is to look up a date information for each element inside the $droppedStudentIds array. I checked the MySQL query in itself and it produces that desired result.

Thanks!

2
  • 2
    The only explanation is that your query isn't actually returning any rows. Try echoing $query3 and then pasting that query into phpmyadmin or mysql. Commented Sep 15, 2013 at 3:04
  • 1
    ClassListDate is empty? Commented Sep 15, 2013 at 3:05

2 Answers 2

1

You're assigning to the array you're looping through on this line:

 $droppedStudentIds[$value][] = $row['ClassListDate'];

This could be causing your script to timeout, which would be why you're not seeing any output.

I'd add a second array to avoid conflicts and use that for storing results from the query, e.g.

$temp[$value] = $row['ClassListDate'];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Daniel, I tried your suggestion but I get an "undefined variable" error for $temp. Hmm I must be missing something obvious...
That's just an initialisation warning. Before entering the loop, add the line '$temp = array();'
0

Thanks all for the response, it helped pin down the mistake!

  1. No need for a while loop when asking for a single record
  2. The query was actually incorrect. The subselect was getting the maximum date in the database, regardless of whether the StudentId was present or not. The correct query is the following:

    select ClassListDate from student_classlists where StudentId = '$value' and ClassListDate = (select max(ClassListDate) from student_classlists where StudentId = '$value')

Thanks again!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.