Jump to content

Recommended Posts

Hello,

I 've been coding in php for some time now but am not sure where the end bracket on a while loop is supposed to be situated.

For example:

$get = mysql_query("SELECT * FROM members WHERE username='$user'");
while($row = mysql_fetch_array($get))
{
$balance = $row["balance"];
}
$new_balance = $balance + $deposit;

 

OR is the above script supposed to be like this (the end bracket has changed position)

 

$get = mysql_query("SELECT * FROM members WHERE username='$user'");
while($row = mysql_fetch_array($get))
{
$balance = $row["balance"];

$new_balance = $balance + $deposit;
}

Link to comment
https://forums.phpfreaks.com/topic/233711-while-end-bracket/
Share on other sites

well that depends..

 

Both ways are fine. However.. if there is balance's to add then within the while loops brackets you'd want to put your statement. If there is only one balance to add to a deposit then outside is best.. All depends on what you want to do in the end with the data..

Link to comment
https://forums.phpfreaks.com/topic/233711-while-end-bracket/#findComment-1201527
Share on other sites

That depends on if you want the $new_balance variable upadated with the value of $deposit every time a record is returned by the query (put it inside the while{}), or if you just want the $deposit added once to the last value of $balance retrieved from the database (put it outside the while {}).

 

What I expect you are trying to do, and where the confussion is coming from, is that you want to sum the balance and then add the deposit.  if that is the case then neither code snipit is going to work.

Link to comment
https://forums.phpfreaks.com/topic/233711-while-end-bracket/#findComment-1201529
Share on other sites

the first end bracket would work where you have defined $balance it will allways set to what the last row output from your whale statement is

$balance + $row['balance']; will add the total balance from all users same goes for $new_balance = $balance + $deposit; if you have it outside the last } wil it add the totals

Link to comment
https://forums.phpfreaks.com/topic/233711-while-end-bracket/#findComment-1201536
Share on other sites

the first end bracket would work where you have defined $balance it will allways set to what the last row output from your whale statement is

$balance + $row['balance']; will add the total balance from all users . . .

 

Yeah, but that's not how you want to sum the values, it's much better to do it in the SQL beforehand, rather than run through a loop.

Link to comment
https://forums.phpfreaks.com/topic/233711-while-end-bracket/#findComment-1201543
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.