我正在尝试从 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;
}}
// ?>
最佳答案
execute()
SQL 后,您必须获取
结果集。
$count->execute();
$sumAmount = 0;
while($row = $count->fetch()) {
$sumAmount = $row[0];
}
echo $sumAmount;
请注意,即使您的结果集仅包含一行,这里也有一个 while 循环。 while 循环的存在是为了尊重许多 SQL 查询返回多行的事实。
关于php - php中mysql中两个日期之间的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54276847/