1

This is my code

$stmt = $conn->prepare("SELECT tmdb_movies.movie_title,tmdb_movies.tmdb_id, count (tmdb_id),GROUP_CONCAT(DISTINCT genres.genres_name) AS genres_name

FROM tmdb_movies

JOIN genres USING (tmdb_id)

GROUP BY tmdb_movies.movie_title,tmdb_movies.tmdb_id

HAVING find_in_set('$category1', genres_name) AND find_in_set('$category2', genres_name) 

LIMIT $limit OFFSET $start"); 


     // Then fire it up
     $stmt->execute();

 // Pick up the result as an array
 $result = $stmt->fetchAll();

Since i cannot calculate total number of rows which include $category1 and $category2 in the same code, I added this code before it.

$sql = "SELECT count(tmdb_id),group_concat(genres.genres_name) AS genres_name

 FROM `tmdb_movies` 

JOIN genres USING (tmdb_id)
GROUP BY tmdb_movies.tmdb_id
HAVING find_in_set('$category1', genres_name) AND find_in_set('$category2', genres_name) "; 

$result = $conn->prepare($sql); 
$result->execute(); 
$totalrows = $result->rowCount();
echo $totalrows;

But $totalrows echo 3 here. But their a lot more rows than 3.

Josh Poor
  • 505
  • 1
  • 5
  • 12
  • Possible duplicate of [Find total number of results in mySQL query with offset+limit](https://stackoverflow.com/questions/5928611/find-total-number-of-results-in-mysql-query-with-offsetlimit) – CBroe Jun 30 '17 at 07:52

4 Answers4

1

you are overriding the $stmt after execute using fetchAll()

so instead try this

$stmt->execute();
 $totalrows = $stmt->rowCount(); 
 echo $totalrows;
 // Pick up the result as an array
 $result = $stmt->fetchAll();
Exprator
  • 26,992
  • 6
  • 47
  • 59
0

Try rowCount() for count number of rows

$totalrows = $stmt->rowCount();
Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
0

try this

$stmt = $conn->prepare("SELECT SQL_CALC_FOUND_ROWS tmdb_movies.movie_title,tmdb_movies.tmdb_id, count (tmdb_id),GROUP_CONCAT(DISTINCT genres.genres_name) AS genres_name

FROM tmdb_movies

JOIN genres USING (tmdb_id)

GROUP BY tmdb_movies.movie_title,tmdb_movies.tmdb_id

HAVING find_in_set('$category1', genres_name) AND find_in_set('$category2', genres_name) 

LIMIT $limit OFFSET $start"); 


     // Then fire it up
     $stmt->execute();

 // Pick up the result as an array
 $result = $stmt->fetchAll();
 $conn->prepare('SELECT FOUND_ROWS() as COUNT');
 $conn->execute();
 $count = $conn->fetchColumn();
 echo $count; //here's your total count
perseusl
  • 348
  • 1
  • 14
0
    //This is the best answer ever to this questions since mysqli is deprecated     
    //this is the code to get the total number of rows
         $handler = new PDO(DB_HOST, DB_USERNAME, DB_PASSWORD); $stmt = $handler->prepare('SELECT COUNT(*) AS totalRows FROM table_name '); 
         $stmt->execute(); 
         $row = $stmt->fetch(PDO::FETCH_ASSOC); 
         $totalRows = $row['totalRows']; ?> 
         //this prints the total number of rows
        echo $totalRows; 
People
  • 1
  • 3