Count rows from mysql_query results

If I have this:

$results = mysql_query("SELECT * FROM table_name WHERE id=$id");

Is there a way to check how many rows the Private or Company fields value?

I need to show the user how many "Private" and "Company" records were found without making another request. (There is a column named "ad_type" that contains either "Private" or "company")

I already know mysql_num_rows for counting all rows!

EDIT: There are 500 thousand records! So maybe the iteration as a result is slow, what do you think?

Thanks for the help:)

+3
source share
10 answers

, , .

( Gal)

$results = mysql_query("SELECT *,(SELECT COUNT(*) FROM table_name WHERE column=$value) count FROM table_name WHERE id=$id");

, , , , SQL- , .

:

$results = mysql_query("SELECT * FROM table_name WHERE column=$value");
$num_rows = mysql_num_rows($result);

, .

+10

- :

$results = mysql_query("SELECT *,(SELECT COUNT(*) FROM table_name WHERE column=$value) count FROM table_name WHERE id=$id");

sql.

+3

,

$results = mysql_query("SELECT * FROM table_name WHERE id=$id");
$count = mysql_num_rows($results);
+2

count():

  • mysql_query(), ,
  • mysql_fetch_array(), 1
  • , ,

, , :

// check whether email used
$check_email_sql = "select count(*) from users where email='$email'";
$row = mysql_fetch_array(mysql_query($check_email_sql));
$email_count = $row[0];
+2

Private Company ad_type, ?

+1

, ? .

+1

SELECT COUNT(*) FROM table_name WHERE id=$id GROUP BY fieldvalue HAVING fieldvalue = "Private"
SELECT COUNT(*) FROM table_name WHERE id=$id GROUP BY fieldvalue HAVING fieldvalue = "Company"

. , "Private" "Company" .

0

, , .

SELECT ad_type, COUNT(*)
FROM table_name
WHERE (id=$id)
GROUP BY ad_type
HAVING ((ad_type = 'Private') OR (ad_type = 'Company'))

, id = $id, . ( ) , .

0

, :

SELECT ad_type, count(*) FROM table_name WHERE id=$id GROUP BY ad_type; 

HAVING, , , ad_type, ( , , ad_type, ). ; . , , ( ).

!

0

Iterate over the query results and count the number displayed in local variables.

-2
source

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


All Articles