Ankur
Ankur

Reputation: 169

While loop doesn't outputs correct result

I've been working on this application which has a table named funds , this funds table maintains the incoming and outgoing of funds from user account.

Here's the database design :-

NAME          TYPE                       DEFAULT     NULL
recordid      int auto_increment                     NO
parentemail   varchar(255)                           NO
funds         int                                    NO
is_locked     varchar                    YES         NO 

Ok, so suppose there are 4 entries in this table, two of them having as status YES and two of them having NO status, I'm using this query to get the sum and locked status from this table.

SELECT sum(funds) AS funds,is_locked AS status FROM funds where parentemail = '" . $email . "' GROUP BY is_locked

And then I'm using this code to put the value of locked funds (funds with status (is_locked) to YES) and unlocked funds (funds with status (is_locked) set to NO) to different variables;

$query3="SELECT sum(funds) AS funds,is_locked AS status FROM funds where parentemail = '" . $email . "' GROUP BY is_locked";
$result3=mysql_query($query3,$db) or die (mysql_error($db));
$row3=mysql_fetch_array($result3);
$ulfunds=0;
$lfunds=0;
while($row3=mysql_fetch_assoc($result3))
{
    if($row3['status'] == "NO") //if is_locked == NO, then funds are not locked and it would be stored in $ulfunds
    {
        $ulfunds=$ulfunds + $row3['funds'];
    }
    if($row3['status'] == "YES")//if is_locked == YES, then funds are are locked and would be stored in $lfunds
    {
        $lfunds=$lfunds + $row3['funds'];
    }
}

PROBLEM

Now if I echo $lfunds(locked funds) and $ulfunds(unlocked funds), I get correct value for $lfunds (locked funds, is_locked == YES) but I get output 0 (the default value) for the $unfunds (unlocked funds).

How can I fix this so that I should get the correct value in both the variables?

Thanks.

Upvotes: 0

Views: 53

Answers (2)

user3213388
user3213388

Reputation:

You should use either Mysql_fetch_array()

Or

Mysql_fetch_assoc()

But not both. Try putting the loop on mysql_fetch_array() it should work

Upvotes: 0

Krish R
Krish R

Reputation: 22711

Remove this line from your code,

 $row3=mysql_fetch_array($result3);

Upvotes: 2

Related Questions