Author Topic: [SOLVED] Getting the number of rows  (Read 613 times)

0 Members and 1 Guest are viewing this topic.

Offline chantownTopic starter

  • Enthusiast
  • Posts: 134
    • View Profile
    • skoogO
[SOLVED] Getting the number of rows
« on: November 27, 2007, 02:05:26 AM »
Hi,

Let's say i have a query:

"SELECT topic_name FROM mytable WHERE user='$username' LIMIT $Start, $pageSize";

Now i can display the topic_name in a table.

However, I want to know how many TOTAL topics there are from this user, so I can make pages and calculate how many topics on each page (pagination)

How do I do that without using another query like this?--
"SELECT count(topic_name) FROM mytable WHERE user = '$username'";

:) fank you

Offline ~n[EO]n~

  • Devotee
  • Posts: 725
    • View Profile
Re: Getting the number of rows
« Reply #1 on: November 27, 2007, 02:10:11 AM »
through mysql_numrows, it retrieves the number of rows from your query.

Offline chantownTopic starter

  • Enthusiast
  • Posts: 134
    • View Profile
    • skoogO
Re: Getting the number of rows
« Reply #2 on: November 27, 2007, 02:15:26 AM »
Yes, ;) but that will give me the number of rows with the LIMIT applied

I'm looking for the total number of rows

Offline ~n[EO]n~

  • Devotee
  • Posts: 725
    • View Profile
Re: Getting the number of rows
« Reply #3 on: November 27, 2007, 02:24:52 AM »
See if you are doing for pagination then you need 2 queries for e.g.
First
query = SELECT topic_name FROM mytable WHERE user='$username';
numrows = mysql_numrows(query)

here you will get total rows for pagination purpose, then you add the limit again
new_query = query."LIMIT $Start, $pageSize";
result = mysql_query(new_query)

And you will get the limit from here and total rows from above. Hope you got the idea
Tell me if I am wrong...

Offline chantownTopic starter

  • Enthusiast
  • Posts: 134
    • View Profile
    • skoogO
Re: Getting the number of rows
« Reply #4 on: November 27, 2007, 02:35:48 AM »
lol no you're absolutely right

but that's 2 queries (and if it's a full-text search, it's very cpu intensive)

is there any way to do it with 1 query?

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: Getting the number of rows
« Reply #5 on: November 27, 2007, 11:51:03 AM »
Yes... use SQL_CALC_FOUND_ROWS.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline ~n[EO]n~

  • Devotee
  • Posts: 725
    • View Profile
Re: Getting the number of rows
« Reply #6 on: November 27, 2007, 11:59:49 AM »
Yes... use SQL_CALC_FOUND_ROWS.
and what is the equivalent in PHP... i didn't find in the manual  ;D

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: Getting the number of rows
« Reply #7 on: November 27, 2007, 12:40:16 PM »
It's not in the manual.  Read the mysql manual -- it's a modifier to SELECT, like DISTINCT; after you issue your LIMIT query, call SELECT FOUND_ROWS(), and get it back.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline chantownTopic starter

  • Enthusiast
  • Posts: 134
    • View Profile
    • skoogO
Re: Getting the number of rows
« Reply #8 on: November 27, 2007, 02:23:10 PM »
thank you thank you thank you