How to get the sum of time from database? [duplicate]

北城以北 提交于 2020-02-02 12:27:04

问题


I wanted to get the sum of this column in my database and display it in home page.

I use this code to save data on my Database

$timeanddate=$_POST['timeanddate'];
$time=$_POST['tst'];
$name=$_POST['name'];   
$number=$_POST['number'];
$disposition=$_POST['disposition'];
$remarks=$_POST['remarks'];
$times=$_POST['timetoday'];
$dates=$_POST['datetoday'];

if(isset($_POST['submit']))
{
session_start();
$con=mysqli_connect("localhost","root","","hsncs_db");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO hsncs_tbl (id, name, number, call_disposition, remarks, time, date, time_duration, hsncs_user)
VALUES
('', '$_POST[name]', '$_POST[number]', '$_POST[disposition]', '$_POST[remarks]', '$_POST[timetoday]', '$_POST[datetoday]', '$_POST[tst]', '$_SESSION[fname]')";

if (!mysqli_query($con,$sql))
{
  die('Error: ' . mysqli_error($con));
}

mysqli_close($con);

I tried select sum(time_duration) as sum from hsncs_tbl but still doesn't work. please help. thanks!


回答1:


I think you have to retrieve the values of the dates as strings, and convert them in int using strtotime().

Then, sum the values to obtain the sum of the Unix timestamps.

Bye!




回答2:


it might be because us want to save the result of your query AS sum and by nature sum is a Mysql , Sql function.

So it might be a case that the query sees it as a function not a name and expects this sum to intake data,

Best pratice is to avoid using SQL, Mysql functions names as names for your: tables, columns or in your case as a result



来源:https://stackoverflow.com/questions/22529300/how-to-get-the-sum-of-time-from-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!