I can query just fine now and its working great but I am trying to find a way to implement a mysql_num_rows and cannot find a way todo it without first getting all the rows. What I would like is to get the number first and I dont want to have to store several thousand rows before I know how much I will be dealing with.

I have read and reread the protocol but cant find anything could someone help point me in a direction or tell me that the only way would be to get the rows first? Thanks

Generally the database does not know how may rows will be returned unless you do a count(*) or actually fetch all of the rows. In many cases, the DB does not need to build the whole result set before it can start outputting rows to the client, and so it will output rows as you fetch them in more of a streaming fashion. This avoids doing any unnecessary work if you cancel the query before fetching all available results, and also avoids having to keep results in a temporary area until you fetch them.

Certain queries which involve sorting/grouping etc may require the whole result set to be built before it can output anything, in which case fetching the first batch of rows will be slow, and fetching subsequent batches will be faster as all of the heavy lifting has already been done.

That seems sensible (assuming you really need to know the count at all). You cannot guarantee that the count will be the same if you issue two queries (unless you use something like "serializable" isolation level, but I think that has major locking implications in mysql). Fudging the query to somehow return a count would be really ugly as well.

My opinion: The fact that you want a count means that you're not doing proper stream processing of your data.
Perhaps a more efficient way is to find a way to write the application layer without count, instead.

For select count() to return the number of rows that a later select will return, your transaction isolation level needs to be "repeatable-read" or better. However, any isolation level below "serializable" is not fully transaction compliant. If you're using MySQL for an online transaction database, you should be using "serializable." Also, when benchmarking against other databases, you should probably be using this isolation level.