php - How to count records in sql database?

587

I know how to get the number of rows using mysql_num_rows. What i want to do is count each ip address, so i can count how many diffrent ip addresses are in my db. if that makes sense lol. cause there is just over 1,000 records. so let me show you a quick example.

say i have these ip addresses.

127.0.0.1
127.0.0.1
127.0.0.1
127.0.0.2
127.0.0.2
127.0.0.3
127.0.0.3

i want it to count 1 of each. and print 3 or whatever ammount i have in my database not count all which is what mysql_num_rows does and prints 7.

sorry if the title isn't very specific.

760

Answer

Solution:

Use thedistinct andcount features together:

SELECT COUNT(distinct ip_address) FROM some_table;
847

Answer

Solution:

UseDISTINCT to count once. Not involving the duplicates. Try this query:

SELECT COUNT(DISTINCT IP) 
FROM table_name;
332

Answer

Solution:

Use the MySQL Distinct Clause

SELECT DISTINCT IpAddress FROM MyTable
74

Answer

Solution:

SELECT ip , COUNT(ip)
FROM table_name GROUP BY ip

People are also looking for solutions to the problem: PHP use of array_map array_shift to slice array into odd and even sub arrays

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.