I’m still fairly new to mysql and would like to ask for some help.
users table
idusers name other_info
1 Test Last email
2 True Blue phone
3 False Name phone
4 Purpose Test email
likes table
id idusers iditems
1 1 3
2 1 4
3 1 4
4 2 3
5 3 3
6 2 1
Currently I have this table set up in mysql, and the idusers should match in my syntax to get information about that certain user that liked those certain items.
I currently have something like this
$liked_users = $dbh->query("SELECT users.idusers,likes.idusers FROM users,likes WHERE users.idusers = likes.idusers");
This should match up right?
But I’m trying to output some JSON array where this data of users should fall into certain iditems
while($row = $items-> fetch(PDO::FETCH_ASSOC)){
//$get_liked_idusers = $liked_idusers->fetchAll(PDO::FETCH_ASSOC);
//$liked_users ->execute(array($get_liked_idusers));
$json['data'][] = array(
'id'=>$row['iditems'],
"likes" => array(
"data"=>$liked_users -> fetchAll(PDO::FETCH_ASSOC) //users should be displayed only if they liked the item that is going through this while loop only.
)
);
}
So I’m trying to output information regarding users inside the "data" array and it should follow the iditems. And I’m not sure how I would do so, because I don’t think adding “AND iditems = ?" inside my first $liked_users mysql syntax will work as its asking to get two different tables.
I hope my question is clear?
Thanks in advance!
You can do something like this
cause you query should have that many feilds that you want to loop through your json data