0

I'm developing a social media for uploading videos and under a video the user could like/dislike the video. Now, I've created a table called "reactions" with the following rows; video_id, video_reaction, video_author_id. Video_id is the ID of the video, video reaction is a tinyint (1 = like, 0 = dislike) and video_author_id is the ID of who liked/disliked the video. So I'm trying to count the rows to see all the likes, this is what I've done in PHP;

$video_id = 1;

// find out likes
$likes = $link->prepare("SELECT COUNT(*) FROM reactions WHERE reaction = 1 && video_id = ?;
");
$likes->bind_param("s", $video_id);                       
$likes->execute();
 
$result = $likes->get_result();
while ($rows = $result->fetch_assoc()) {
$likes = $rows["??????"];
}

I don't know what to put in the $rows[] to get the number of columns where the reaction is 1 and the video_id is 1. Help me please.

  • Tip: Ditch the `;` and newlines in queries executed using a programmatic interface. – tadman Mar 06 '21 at 14:53
  • 1
    Hint: `COUNT(*) AS count_of_reactions` or whatever you want to call that result, so you can `$rows['count_of_creations']`. You could also `var_dump($rows)` to see what you get. Minor note: This is *one* row, hence `$row` would be more conventional. – tadman Mar 06 '21 at 14:54
  • thanks for your answer, doing var_dump($rows) I get NULL –  Mar 06 '21 at 14:57
  • Is this inside the loop or after the loop? – Nigel Ren Mar 06 '21 at 14:58
  • 1
    You need to turn on your error logging because `&&` is not valid SQL. You need `AND`. Tip: [Enable exceptions](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli). – tadman Mar 06 '21 at 14:59
  • @tadman no it's not inside a loop, inside the loop it becomes array(1) { ["count"]=> int(0) } –  Mar 06 '21 at 15:04
  • Then there's your answer. `'count'`. – tadman Mar 06 '21 at 15:09

0 Answers0