More efficient way to COUNT () MYSQL strings through PHP?

This is how I calculate mysql strings in PHP right now:

//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?

+3
source share
3 answers

No. If you want a count, you run a query that you are already using.

If you need more efficient work, make sure there is an index in the user_id column.

+3
source
//GET total # of results and build $start and $end for main SQL statement
$sql= 'SELECT * FROM savedsearches WHERE user_id=\''.mysql_real_escape_string($user_id).'\'';
$initial_query = mysql_query($sql) or die("SQL error");
$numrows = mysql_num_rows($initial_query)

, mysql_num_rows() mysql_fetch_array. , , . , . : PHP: mysql_num_rows()

0

, ? , , , .

-1

Source: https://habr.com/ru/post/1766589/


All Articles