开发者

How to get sum of mysql column using PHP as usable variable

开发者 https://www.devze.com 2023-03-19 09:01 出处:网络
I\'ve been searching for a week now on how to accomplish this but none of the tutorials have worked - I typically get a message \"resource id 18.\"

I've been searching for a week now on how to accomplish this but none of the tutorials have worked - I typically get a message "resource id 18."

I'm creating a bank simulation game.

End goal: I want a variable "$player_balance" to be the sum of all account balances owned by that player so that it can be displayed at the bottom of the table under account balances.

Here is my code, thanks for any help or direction that you can provide.

function displayMyAccounts(){
   global $database, $session;
   $q = "SELECT game_account_number,game_account_owner,game_account_name,game_account_balance FROM ".TBL_ACCOUNTS." WHERE game_account_owner='".$session->username."'";
   $result = $database->query($q);
   /* Error occurred, return given name by default */
   $num_rows = mysql_numrows($result);
   if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }

   /* Display table contents */
   echo "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\" width=\"100%\">\n";
   echo "<tr><td><b>Account Number</b></td><td><b>Account Name</b></td><td><b>Balance</b></td></tr>\n";
   for($i=0; $i<$num_rows; $i++){
      $anumber  = mysql_result($result,$i,"game_account_number");
      $aowner = mysql_result($result,$i,"game_account_owner");
      $aname  = mysql_result($result,$i,"game_account_name");
      $abalance   = mysql_result($result,$i,"game_account_balance");
      setlocale(LC_MONETARY, 'en_US');
      $abalance2 = money_format('%(#10n', $abalance);

      echo "<tr><td>$anumber</td><开发者_如何学Python;td>$aname</td><td>$abalance2</td></tr>\n";
   }
       echo "<tr><td></td><td></td><td>$player_balance</td></tr>\n";
   echo "</table><br>\n";
}
displayMyAccounts();

The above code is what appears on each player's "account page." I want the sum of their accounts to appear on the last row. Thanks for any help, I'll continue searching and trying in the meantime.

Here is the output based on the above:

    Account Number  Account Name    Balance
    1000083690  Maverick    $ 50,000.00
    1000083696  WellsFargo  $ 50,000.00
    1000083697  Wachovia    $ 50,000.00


Don't use mysql_result(). It's slow and inefficient, and what you're doing is better done with mysql_fetch_assoc():

$player_balance = 0;
// Also, ditch the for loop. This is the typical convention for retrieving results.
while ($row = mysql_fetch_assoc($result)) {

  $abalance1 = money_format('%(#10n', $row['game_account_balance']);
  echo  "<tr><td>{$row['game_account_number']}</td><td>{$row['game_account_name']}</td><td>$abalance1</td></tr>\n";

  // Now add to the balance
  $player_balance = $player_balance + $row['game_account_balance'];
}
// And output your balance
$abalance_sum = money_format('%(#10n', $player_balance);
echo "<tr><td></td><td></td><td>$abalance_sum</td></tr>\n";


resource error means YOU DO NOT HAVE THE TABLE OR THE PERMISSION, OR YOU ARE TRY TO ACCESS A COLUMN WHICH DOES NOT EXIST

0

精彩评论

暂无评论...
验证码 换一张
取 消