dshfjsh_5455
2017-09-06 13:01 阅读 7
已采纳

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

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????
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dousongxuan7507 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.

    点赞 评论 复制链接分享

相关推荐