dshfjsh_5455
2017-09-06 13:01
浏览 11
已采纳

为什么这个函数没有返回正确的结果?

I'm fetching data from database by creating two functions "daySum()" and "yesterdaySum()". When calling those functions it returns correct values, 54000 and 51000 respectively.

Then I put them into variables $day and $yesterday, but when doing variable operation "$day+$yesterday" it prints 0. This won't allow me to create a conditional which is needed for my project.

Can you help me out?

Thanks in advance!

//DEFINING 2 FUNCTIONS: daySum() and yesterdaySum()

function daySum() {
    //connect to database
    require 'connect.php';

    // query
    $query = "SELECT sum(till_amount) FROM Cash ";
    $query .= "WHERE date ='2017-09-05'";
    $result = mysqli_query($connect, $query);

    // fetch
    while($row = mysqli_fetch_assoc($result)) {
        print_r($row["sum(till_amount)"]);
    }
}

function yesterdaySum() {
    //connect to database
    require 'connect.php';

    // query
    $query = "SELECT sum(till_amount) FROM Cash ";
    $query .= "WHERE date ='2017-09-04'";
    $result = mysqli_query($connect, $query);

    // fetch
    while($row = mysqli_fetch_assoc($result)) {
        print_r($row["sum(till_amount)"]);
    }
}

// PUTTING THEM INTO VARIABLE 

$day = daySum(); // sum is 54000
$yesterday = yesterdaySum(); // sum is 51000

    // adding $day plus $yesterday should print 105000
    // and here is the problem...

print($day + $yesterday); // it prints 0

//WHY????

图片转代码服务由CSDN问答提供 功能建议

我通过创建两个函数“daySum()”和“yesterdaySum()”从数据库中获取数据。 调用这些函数时,它分别返回正确的值54000和51000。

然后我将它们放入变量$ day和$昨天,但是当做变量操作“$ day + $ yesterday”时它会打印0.这将不允许我创建条件,这是 我的项目需要。

你能帮助我吗?

提前致谢!

  //定义2个函数:daySum()和yesterdaySum()
 
函数daySum(){
 //连接数据库
需要'connect.php'; 
 
 //查询
 $ query =  “SELECT sum(till_amount)FROM Cash”; 
 $ query。=“WHERE date ='2017-09-05'”; 
 $ result = mysqli_query($ connect,$ query); 
 
 // fetch  
 while($ row = mysqli_fetch_assoc($ result)){
 print_r($ row [“sum(till_amount)”]); 
} 
} 
 
函数yesterdaySum(){
 //连接到 database 
 require'connect.php'; 
 
 // query 
 $ query =“SELECT sum(till_amount)FROM Cash”; 
 $ query。=“WHERE date ='2017-09-04'”  ; 
 $ result = mysqli_query($ connect,$ query); 
 
 // fetch 
 while($ row = mysqli_fetch_assoc($ result)){
  print_r($ row [“sum(till_amount)”]); 
} 
} 
 
 //将它们放入VARIABLE 
 
 $ day = daySum();  // sum是54000 
 $ yesterday = yesterdaySum();  //总和是51000 
 
 //添加$ day加$昨天应该打印105000 
 //这就是问题... 
 
print($ day + $ yesterday);  //它打印0 
 
 //为什么???? 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dousongxuan7507 2017-09-06 13:08
    已采纳

    Your function are just printing things.That's why it's add is not working.

    You have to change your code like below (some improvement also which is commented):-

    <?php
    require 'connect.php'; //don't add multiple time
    function daySum() {
        $query = "SELECT sum(till_amount) FROM Cash WHERE date ='2017-09-05'";
        $result = mysqli_query($connect, $query);
        while($row = mysqli_fetch_assoc($result)) {
            return $row["sum(till_amount)"]); //return value
        }
    }
    
    function yesterdaySum() {
        $query = "SELECT sum(till_amount) FROM Cash WHERE date ='2017-09-04'";
        $result = mysqli_query($connect, $query);
        while($row = mysqli_fetch_assoc($result)) {
           return $row["sum(till_amount)"]); //return value
        }
    }
    
    $day = daySum(); // now $day have value
    $yesterday = yesterdaySum(); //now $yesterday have value
    
    
    print((int)$day + (int)$yesterday); // it will work fine now
    

    Now most easy is to use BETWEEN query:-

    <?php
    require 'connect.php'; //don't add multiple time
    function daySum() {
        $query = "SELECT sum(till_amount) FROM Cash WHERE date BETWEEN '2017-09-04' AND '2017-09-05'";
        $result = mysqli_query($connect, $query);
        while($row = mysqli_fetch_assoc($result)) {
            return $row["sum(till_amount)"]);
        }
    }
    
    $day = daySum();
    
    print($day);
    

    Note:- May be it's you homework task, but start learning about prepared statements today on-ward to prevent your query from SQL INJECTION.It's important and necessary.

    打赏 评论

相关推荐 更多相似问题