tags:

views:

60

answers:

3

Here is how I am currently counting mysql rows in PHP:

//GET total # of results and build $start and $end for main SQL statement
$sql= 'SELECT COUNT(*) FROM savedsearches WHERE user_id=\''.mysql_real_escape_string($user_id).'\'';
$initial_query = mysql_query($sql) or die("SQL error");
$num_sql = mysql_fetch_array($initial_query);
$numrows = $num_sql[0];
mysql_free_result($initial_query);

Is there a more efficient way to do this?

A: 

You mean, do it in fewer lines of code? You could put that functionality into a user-defined function and then call the function instead of using that code, but other than that, it's not going to get an awful lot terser.

Hammerite
A: 

Check this: http://tr.php.net/manual/en/function.mysql-num-rows.php

Onatm
If the developer doesn't want to load the entire result from the database then this method is inefficient.
Andrew Dunn
you are right about it. i guess i totally misunderstood the question. use this method if you already have the results from mysql_query().
Onatm
+1  A: 

No. If you want a count , you run the query you're already running.

If you need more efficiency, make sure there is an index on the user_id column

nos