我知道如何连接,显示,删除,从我的数据库中添加数据,并在我的网站上显示所有这些数据。一切正常。我在我的网站和数据库中看到我的结果。我这样做与AngularJS, AJAX和PHP,但我的问题是我不知道如何显示MYSQL SUM()我的专栏之一。例如,这是我在mysql中的表,我想显示money列的总金额。
id money
1 23
2 345
3 111
这是我得到的:
index . html:
..................
<table class="table table-striped">
<tr>
<th>Money</th>
</tr>
<tr>
<td>{{ wcdrates.sumMoney }}</td>
</tr>
</table>
.................
我不知道我应该让ng-repeat保持原样还是使用不同的东西。总之,重复是行不通的。它显示的是钱栏的所有结果,而不是总和。............
controller.js
.....................
$http.post('./php/showMoney.php')
.success(function(data) {
$scope.wcdrates = data;
})
.error(function(err) {
$log.error(err);
})
....................
connectDatabase.php
<?php
define("__HOST__", "127.0.0.1");
define("__USER__", "root");
define("__PASS__", "");
define("__BASE__", "davbaza");
class DB {
private $con = false;
private $data = array();
public function __construct() {
$this->con = new mysqli(__HOST__, __USER__, __PASS__, __BASE__);
if(mysqli_connect_error()) {
die("DB connection failed:" . mysqli_connect_error());
}
}
public function qryPop() {
$sql = "SELECT * FROM `wcdrates` ORDER BY `id` DESC";
$qry = $this->con->query($sql);
if($qry->num_rows > 0) {
while($row = $qry->fetch_object()) {
$this->data[] = $row;
}
} else {
$this->data[] = null;
}
$this->con->close();
}
public function qryFire($sql=null) {
if($sql == null) {
$this->qryPop();
} else {
$this->con->query($sql);
$this->qryPop();
}
//$this->con->close();
return $this->data;
}
}
?>
showMoney.php
<?php
include('connectDatabase.php');
$db = new DB();
$sql = "SELECT SUM(money) as sumMoney FROM wcdrates";
$data = $db->qryFire();
echo json_encode($data);
mysql CREATE TABLE IF NOT EXISTS `wcdrates` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`money` int(11) NOT NULL,
PRIMARY KEY (`id`)
)
我不知道该怎么做才能得到我想要的。
将代码中的qryPop
更改为以下代码:
$sql = "SELECT id,money , SUM(money) as sumMoney FROM `wcdrates` ORDER BY `id` DESC";
希望你能得到答案