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.

1

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

12 Comments

It echo the movie title of the 1st row
Fatal error: Uncaught Error: Call to undefined method PDOStatement::fetchRow() in
Here answer is 10, because Limit is 10.
so what you want? it will return according to limit, if you need all remove the limit from query
I cannot remove limit. I want to count all the rows in my database to make pagination system. So i need to make another select statement to count this without limit?
|
0

Try rowCount() for count number of rows

$totalrows = $stmt->rowCount();

2 Comments

Here answer is 10, because Limit is 10.
Updated my question, see now
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

4 Comments

Fatal error: Uncaught Error: Call to undefined method PDOStatement::prepare() in
Fatal error: Uncaught Error: Call to undefined method PDO::execute() in . In my code $conn connects to the database
did you add the SQL_CALC_FOUND_ROWS in your original statement?
Now, i did...but Fatal error: Uncaught Error: Call to undefined method PDO::execute() in line 149. This is line 149 ` $conn->execute();`
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; 

Comments

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.