Select Select - 2 months ago 6
MySQL Question

PHP + SQL -- Counting clicks in multiple rows

i stuck at the problem that my code wont count total clicks in multiple rows.

This is how the Database looks

id | jb_clicks | created_time
--------------------------------------------------
1 | 14 | 1475420816
2 | 7 | 1475422200
3 | 9 | 1475422217
4 | 3 | 1475422239


I want the result to be 33 (14+7+9+3)

How is this possible?

The current code looks like this:

SELECT COUNT(*) AS jb_clicks FROM jb_urls


my function:

function sumdatabase($select, $statement){
$config = new mysql_config;

$link = mysqli_connect($config::MYSQL_HOST,$config::MYSQL_USER,$config::MYSQL_PASS);
mysqli_select_db($link, $config::MYSQL_DATABASE);

$result = mysqli_query($link,"SELECT SUM(".$config::MYSQL_PREFIX."$select) AS jb_clicks FROM $statement");
$row = mysqli_fetch_assoc($link,$result);

return $row[$config::MYSQL_PREFIX.$select];
}

Answer

Use the following:

SELECT SUM(jb_clicks) AS jb_clicks FROM jb_urls

COUNT() is used to count the row numbers and SUM() is to calculate the total sum of a column in a table. In your case, it would be SUM(jb_clicks).