user1222728
user1222728

Reputation: 187

Get total number of rows meeting WHERE condition

I'm making a search application, and I want to figure out how many results a certain query fetches in order to calculate how many pages of results there are. I basically have:

mysql_query("SELECT * FROM table WHERE conditions LIMIT $page*$items, $items");

Do I have to do a query w/o the limit clause to get the total number, or is there a better way? Thanks!

Upvotes: 0

Views: 674

Answers (4)

hkf
hkf

Reputation: 4520

SELECT COUNT (*) FROM table WHERE conditions

Upvotes: 4

Naveen Kumar
Naveen Kumar

Reputation: 4601

$page_count=10 // no of rows per page 
$record=mysql_query("select count(*) from table where conditions");
$row=mysql_fetch_row[0];

$no_of_pages = ceil($row/$page_count);

Upvotes: 0

Mosty Mostacho
Mosty Mostacho

Reputation: 43434

I see you're using PHP. Once you've run the query, why don't you mysql_num_rows in PHP? That way you won't need to run two queries.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798566

SELECT COUNT(*)
  FROM table
  WHERE conditions

Upvotes: 3

Related Questions