Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count all values from column values in MySQL using PHP

I have a MySQL database and in one column, I have numbers (price) and I need to count all numbers in this column with PHP... How can I do that?

like image 986
Štěpán Němejc Avatar asked Jul 03 '26 11:07

Štěpán Němejc


2 Answers

If you want to count the number of columns use:

Select count(column_name) FROM table name.

If you want the sum of column values then use:

Select SUM(column_name) FROM table name.
like image 134
Prasannjit Avatar answered Jul 06 '26 02:07

Prasannjit


UPDATE with PHP Code

If you want to count you can do it like this:

    $query = "SELECT COUNT(*) FROM table_name"

    // Print out result
    while($row = mysql_fetch_array($result)){
    echo "Total count". $row['COUNT(*)'];
}

If you want to get the sum of one column, you can do like this:

$query = "SELECT SUM(column_name) FROM table_name"
// Print out result
    while($row = mysql_fetch_array($result)){
    echo "Total sum". $row['SUM(column_name)'];
}
like image 40
Erman Belegu Avatar answered Jul 06 '26 01:07

Erman Belegu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!