1,105,450 Community Members

PHP Framework and Fetch Assoc

Member Avatar
joshmac
Junior Poster
119 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

I am using a framework, and I am having an issue with getting the results of this particular associative array. All others work except for this one and I am not sure why. This one only returns one result. Here is what I have in my model:

    public function listStats($id) {
        $sql = DB::inst()->query( "SELECT * 
            FROM ".
                TP."tracking 
            WHERE 
                mID = '$id' 
            GROUP BY 
                mID" 
        );
        $data = array();
        if($sql->num_rows > 0) {
            while($row = $sql->fetch_assoc()) {
                $data[] = $row;
            }
            return $data;
        }
    }

And here is what I have in my view:

        <?php foreach($this->listStats as $k => $v) : ?>
        <tr>
            <td>
                <?php echo clean($v['email']); ?>
            </td>
        </tr>
        <?php endforeach; ?>

Any help with figuring this out is greatly appreciated.

Member Avatar
veedeoo
Veteran Poster
1,016 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 166 [?]
Skill Endorsements: 17 [?]
 
1
 

Hi,

What framework are you using?

can you var_dump or print_r this ?

listStats($id);

and this

$this->listStats

what do you get?

I am assuming that $this->listStats is a method of the view class?

Member Avatar
veedeoo
Veteran Poster
1,016 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 166 [?]
Skill Endorsements: 17 [?]
 
0
 

try this first... put this before the closing bracket of the method listStats;;

    return $data;
    } // closing bracket of method listStats..

like this ..

    public function listStats($id) {
$sql = DB::inst()->query( "SELECT *
FROM ".
TP."tracking
WHERE
mID = '$id'
GROUP BY
mID"
);
$data = array();
if($sql->num_rows > 0) {
while($row = $sql->fetch_assoc()) {
$data[] = $row;
}

}

    return $data;
}
Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Also, since you are querying a specific mID and then grouping by the same field, it's correct to get just a row. So try to change the GROUP BY clause. Bye!

Member Avatar
joshmac
Junior Poster
119 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

HMM, the GROUP BY was a later addition because it wasn't working, but removing it makes it work now. Thanks.

Question Answered as of 1 Year Ago by veedeoo and cereal
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: