使用mysqli和php的列总和 [英] Sum of column in using mysqli and php

查看:107
本文介绍了使用mysqli和php的列总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要得到一个列的总和并使用这个代码在我的页面上显示它但是当它应该返回23时它会一直返回1。我检查我的sql语句,它工作正常。这是我正在使用的代码。 (注意:我的服务器是带有php的iis)

 <?php 
require('connection.php') ;

$ sql =SELECT sum(amount)as total FROM td;

$ result = mysqli_query($ sql);

while($ row = mysqli_fetch_assoc($ result)){echo $ row ['total'];}

mysqli_close($ con);
?>

好,所以我添加了while的东西,它只是打破我的代码我得到一个白页。



我现在删除了!在mysqli_query中,我仍然得到一个白页,不知道它是我还是服务器没有好玩。



这可能是无关的,但当我删除了!来自我的其他代码的mysqli_query它打破了它。

 <?php 
需要'connection.php';
$ date = $ _POST ['date'];
$ comment = $ _POST ['comment'];
$ amount = $ _POST ['amount'];

$ sql =INSERT INTO td(日期,评论,金额)VALUES('$ date','$ comment','$ amount');

if(mysqli_query($ con,$ sql))
{
die('Error:'。mysqli_error($ con));
}
echo添加1条记录;

mysqli_close($ con);
?>

一切都修好了谢谢大家!!!

 <?php 
require(' connection.php');

$ sql =SELECT sum(amount)as total FROM td;

$ result = mysqli_query($ sql);

while($ row = mysqli_fetch_assoc($ result))
{
echo $ row ['total'];
}

mysqli_close($ con);
?>

正如上面的评论所说,你不需要!面前的查询方法。


I am tring to get the sum of a column and the show it on my page using this code but it keeps returning a "1" when it should return "23" for example. I check my sql statement and it works fine. This is the code I am using. (note: My server is iis with php)

<?php
require('connection.php');

$sql="SELECT sum(amount) as total FROM td";

$result = mysqli_query($sql);

while ($row = mysqli_fetch_assoc($result)){ echo $row['total'];}

mysqli_close($con);
?>

ok so I added the while thing and it just breaks my code I get a white page.

I now removed the ! in the mysqli_query and I still get a white page, not sure if its me or the server that is not playing nice.

This may be unrelated but when I removed the ! from the mysqli_query from my other code it broke it.

<?php
require 'connection.php';
$date = $_POST['date'];
$comment = $_POST['comment'];
$amount = $_POST['amount'];

$sql= "INSERT INTO td (date, comment, amount) VALUES ('$date', '$comment', '$amount')";

if (mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

Everything is fixed Thanks Everyone!!!

解决方案

Try this:

<?php
require('connection.php');

$sql="SELECT sum(amount) as total FROM td";

$result = mysqli_query($sql);

while ($row = mysqli_fetch_assoc($result))
{ 
   echo $row['total'];
}

mysqli_close($con);
?>

As said in a comment above, you don't need the ! infront of the query method.

这篇关于使用mysqli和php的列总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆