来自PHP中MySQL的两个日期之间的总和

时间:2019-01-20 13:18:56

标签: php mysql

我正在尝试从MySQL数据库获取金额的总和。那我要打印什么?这是我尝试的PHP代码,但未得到任何结果。请告诉我我从数据库中获取总金额的代码。 在MySQL中,“金额”中有一列,而“日期”中是另一列。

<?Php

?>
<html>
<head>
    <style>
                @media print {
  #printPageButton {
    display: none;
  }
  #another {
      display: none;
  }
}
.border {
  border-style: double;
  border-color: blue;
}
</style>
<title>Demo of Search Keyword using PHP and MySQL</title>
</head>

<body>
<?Php
error_reporting(0);
include "config_1.php";
echo "<form  method=post action='getcollectionbagnan.php' ><input type=hidden  name=todo value=search>
<input type=date  name=search_text value='$search_text' ><br>
<input type=hidden  name=todo2 value=search2>
<input type=date  name=search_text value='$search_text2' ><input type=submit value=Search><br>
</form>
";
$todo=$_POST['todo'];
$search_text=$_POST['search_text'];
$todo2=$_POST['todo2'];
$search_text2=$_POST['search_text2'];
if(strlen($serch_text) > 0){
if(!ctype_alnum($search_text)){
echo "Data Error";
exit;
}
}
if(isset($todo) and $todo=="search" and isset($todo2) and $todo2=="search2"){
$type=$_POST['type'];
$search_text=ltrim($search_text);
$search_text=rtrim($search_text);
$search_text2=ltrim($search_text2);
$search_text2=rtrim($search_text2);
    if($type<>"any"){
$query="select sum(amount) from billbagnan where date between '$search_text' and '$search_text2'";
$count=$dbo->prepare($query);
$count->execute();
$no=$count->rowCount();
echo $count;
echo $no;
}}
// ?>

1 个答案:

答案 0 :(得分:0)

execute() SQL之后,必须fetch结果集。

$count->execute();
$sumAmount = 0;
while($row = $count->fetch()) {
    $sumAmount = $row[0];
}
echo $sumAmount;

请注意,即使您的结果集仅包含一行,这里也会有一个while循环。 while循环考虑到许多SQL查询返回多行这一事实。