MySQL Question

how to sum each column in mysql with php?

I'm trying to sum all the columns from a table but its not working.
This is my query to the DB:

$sql = mysql_query("SELECT SUM(clicks) as total FROM urls WHERE member_id='$mem_id'");
$row = mysql_fetch_array($sql);
$sum = $row['total'];

When I try to run the script nothing is working. I tried also with while loop and I get all the results from column clicks on a new line and I cannot sum them.
Where I am doing a mistake ?

Answer Source

Try this. mysqli_ functions are better.

$con = mysqli_connect($mysql_hostname.':'.$mysql_port, $mysql_username, $mysql_password, $mysql_database);

$query = "SELECT SUM(clicks) as total FROM urls WHERE member_id='$mem_id'";

$result = mysqli_query($con, $query);

$row = $result->fetch_assoc();

$sum = $row["total"];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download