0

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.

5
  • You don't need to add the row values to the variables. There's only one row for each status, you can just do an ordinary assignment. Commented Jan 31, 2014 at 20:40
  • possible duplicate of Array returns 1 less row - PHP Commented Jan 31, 2014 at 20:43
  • I recommend you check out Laravel. Commented Jan 31, 2014 at 20:46
  • @MichaelCalkins I once gave a thought to shift to a framework. But I guess I need to have good practice of OOP right? Commented Jan 31, 2014 at 21:27
  • @Ankur If I were to start in PHP all over again I'd just start the right way from the beginning. I went down the path you're on right now and it would have been nice just to have skipped it. Commented Jan 31, 2014 at 23:59

2 Answers 2

2

Remove this line from your code,

 $row3=mysql_fetch_array($result3);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.