0

Here is my query:

$query = "SELECT last_name, first_name, DOB, site, COUNT(*) AS Num FROM student "
    . "WHERE site = '{$siteRow['site']}' "
    . "GROUP BY last_name, first_name, DOB having Num > 1";

When I run this on a MySQL client, I get the appropriate rows. last_name, first_name, DOB, site, and Num

However, when I try to do this (and testing with a var_dump) thru php, I get those same five col;umns, but before each one, I get 0,1,2,3, and 4 - each one corresponding with the 5 columns above in the order written.

Here is php:

$result = $DB->query($query);
$cnt = $DB->count($result);
if ($cnt > 0) {
            while ($row = $DB->fetch_array($result)) { // solved; issue here!
                    if (!in_array(strtolower(trim($row['site'])), $dup_student_sites)) {
                    $records[] = $row;  
                    $dup_student_sites[] = strtolower(trim($row['site']));
                    $dupCount++;
                }

            }
        }


var_dump($records);  // tested here to see result with row numbers

Here is the output of the var_dump (partial):

array(111) {
  [0] =>
  array(10) {
    [0] =>
    string(7) "---"
    's_s_last_name' =>
    string(7) "---"
    [1] =>
    string(4) "---"
    's_s_first_name' =>
    string(4) "---"
    [2] =>
    string(10) "2003-03-20"
    's_s_DOB' =>
    string(10) "2003-03-20"
    [3] =>
    string(8) "---"
    's_s_site' =>
    string(8) "---"
    [4] =>
    string(1) "2"
    'Num' =>
    string(1) "2"
  }

1 Answer 1

3

I usually don't give such watered-down answers but:

The likely issue is $DB->fetch_array

The likely solution is to change it to $DB->fetch_assoc

Explanation

Same idea as this answer:

https://stackoverflow.com/a/9540590/2191572

Sign up to request clarification or add additional context in comments.

5 Comments

As you typed this I realized that was the issue. Marking you correct when time allows.
Haha I appreciate it. I am going to expand on my answer a bit just for future visitors
Sure I can go look it up in the manual, but I always get those two confused; so any simple explanation is always appreciated!
I would recommend going as far as looking for all instances of fetch_array in your web-app and switching it to fetch_assoc granted that you aren't displaying the results using $row[0] and such
After reading that link you posted I completely get it now and see what happened above. Thanks again.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.