chantown Posted November 27, 2007 Share Posted November 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/79048-solved-getting-the-number-of-rows/ Share on other sites More sharing options...
~n[EO]n~ Posted November 27, 2007 Share Posted November 27, 2007 through mysql_numrows, it retrieves the number of rows from your query. Quote Link to comment https://forums.phpfreaks.com/topic/79048-solved-getting-the-number-of-rows/#findComment-400060 Share on other sites More sharing options...
chantown Posted November 27, 2007 Author Share Posted November 27, 2007 Yes, but that will give me the number of rows with the LIMIT applied I'm looking for the total number of rows Quote Link to comment https://forums.phpfreaks.com/topic/79048-solved-getting-the-number-of-rows/#findComment-400062 Share on other sites More sharing options...
~n[EO]n~ Posted November 27, 2007 Share Posted November 27, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/79048-solved-getting-the-number-of-rows/#findComment-400068 Share on other sites More sharing options...
chantown Posted November 27, 2007 Author Share Posted November 27, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/79048-solved-getting-the-number-of-rows/#findComment-400072 Share on other sites More sharing options...
fenway Posted November 27, 2007 Share Posted November 27, 2007 Yes... use SQL_CALC_FOUND_ROWS. Quote Link to comment https://forums.phpfreaks.com/topic/79048-solved-getting-the-number-of-rows/#findComment-400357 Share on other sites More sharing options...
~n[EO]n~ Posted November 27, 2007 Share Posted November 27, 2007 Yes... use SQL_CALC_FOUND_ROWS. and what is the equivalent in PHP... i didn't find in the manual Quote Link to comment https://forums.phpfreaks.com/topic/79048-solved-getting-the-number-of-rows/#findComment-400365 Share on other sites More sharing options...
fenway Posted November 27, 2007 Share Posted November 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/79048-solved-getting-the-number-of-rows/#findComment-400380 Share on other sites More sharing options...
chantown Posted November 27, 2007 Author Share Posted November 27, 2007 thank you thank you thank you Quote Link to comment https://forums.phpfreaks.com/topic/79048-solved-getting-the-number-of-rows/#findComment-400459 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.