Travis Bickle Travis Bickle - 4 months ago 94x
PHP Question

How to display SUM of column in MySQLi and PHP

I have a MySQL database called 'test' with a table called 'week' which has two columns, one called 'address' and another called 'amount'.

I want to get the SUM of column 'amount' and display the result on the screen.

I promise I've looked all over the place for two days, but I am getting nowhere - I am very noob to PHP and MySQLi, so if anyone could point me to some example code to achieve this, I would be very grateful... thanks.


Notice mysq_* is not safe to use (sql injection and stuff), but if you are not going to use user input, it might be OK.

    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    $result = mysql_query("select sum(amount) as total from test.week;", $link);
    if ($row = mysql_fetch_assoc($result) {
        echo "The sum is: " . $row["total"];