HOME       >>       Programming

Cant Find The Error


In this code I want to retrieve the energy and max energy of a players account, then add 8% to energy of maxenergy, hen if energy is bigger than maxenergy, energy is then made equal to maxenergy. Then the database is updated.I don't understand why it's not working.

$query=mysql_query("SELECT MAX(id) as maxid FROM `accounts` LIMIT 1");$row = mysql_fetch_assoc($query);$id = $row['maxid']; $query = "SELECT * FROM `accounts`"; $result=mysql_query($query);while ($row=mysql_fetch_assoc($result)) {$energy = $row['energy'];$maxenergy = $row['maxenergy'];}for($i=1; $i<="$id"; $i++) {$query = "SELECT * FROM `accounts` WHERE (id) = '".$id."'";$result=mysql_query($query);$addenergy = (0.08 * $maxenergy);$energy = ($addenergy + $energy);if($energy > $maxenergy) {$energy = $maxenergy;}$query = "UPDATE `account` SET (energy) = '".$energy."' WHERE (id) = '".$id."'";$result = mysql_query($query);echo" The energy script worked!";}


maybe if you tell us what error messages you get. it would be easier for us to help you ?


EDIT: I THINK that you can't make the energy get higher, or? well, if thats your problem, you should try to add this:


$energy = ($addenergy += $energy);

If thats the fault, then that should be the solution. And remember to write what your problem is, and NOT ONLY what you want to accomplish. because we can hardly guess what your problem is.


And another thing the "the energy script worked" echo, wont work as you think. it will execute whatever happens, becuse you have no if statement


A very usefull trick to debug sql query is to add 'or die(mysql_error())' after every query you make, this way your script will stop once SQL throws an error and the error will be shown to you.


$result = mysql_query($query) or die(mysql_error());

Another usefull trick is to echo variables so you can actualy see their values instead of guessing and hoping.


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.