1

This is my code to select and echo data

Select maintable.movie_title, group_concat(genres.genres_name) AS genres_name
FROM maintable
JOIN genres USING (tmdb_id)
GROUP BY maintable.tmdb_id,maintable.movie_title
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();

I have 2 tables

1.) maintable

2.) genres table

Both tables are linked to each other using tmdb_id

(Please do not ask to show, what I tried. Trust me, it will make the question more confusing)

3
  • You can use conditional aggregation to get a count of the matching rows. Unfortunately, your query is in a mess, so I can't give a formal answer. You are grouping by tmdb_id but you select movie_title, a non aggregate column. This doesn't make any sense. Commented Jun 30, 2017 at 5:58
  • Fixed the question @TimBiegeleisen I just forgot to include that. My real code is 5x bigger than this, but i put only required code Commented Jun 30, 2017 at 6:01
  • See meta.stackoverflow.com/questions/333952/… Commented Jun 30, 2017 at 6:29

2 Answers 2

1

if you need also the count of $totalrows, which have $category1 and $category2 You should use a where in clause

  Select maintable.movie_title, group_concat(genres.genres_name) AS genres_name, count(*) as total_rows
  FROM maintable
  JOIN genres USING (tmdb_id)
  where genres.genres_name in ('$category1', '$category2' )
  GROUP BY maintable.tmdb_id, maintable.movie_title

   LIMIT $limit OFFSET $start

if you need only tital_rows you could select only this value (and use $totalrows = $result->fetchColumn(); ) or in this fecth the column 2 using

 $totalrows = $result->fetchColumn(2); 

or using fetchAll

$result = $stmt->fetchAll();

foreach($result as $key=>$row){

  echo $row['total_rows']  ;

}
Sign up to request clarification or add additional context in comments.

6 Comments

I want to calculate total rows before $limit variable. So i can give a value to it
@JoshPoor what do you mean ? .. i don't understand .. update your question add a proper data sample and the expected result ..
$totalrows = $result->fetchColumn(); how to calculate it here?
I mean how to calculate total rows and transfer the value of it to a variable?
Bro the answer is 4316410442449
|
0

Do you want this?

if ($stmt = $mysqli->prepare($query)) {

    /* execute query */
    $stmt->execute();

    /* store result */
    $stmt->store_result();

    printf("Number of rows: %d.\n", $stmt->num_rows);

    /* close statement */
    $stmt->close();
}

1 Comment

Fatal error: Uncaught Error: Call to a member function rowCount() on array in

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.