right in the below code, i am taking data in from a table (its data type in table is int). but when i put the data in the var, its stored as a string. i need it to be an int as i need to do calculations with it. why is it oding this?
$production_cost, $user_gold are the culprits. im taking data in from the same table, which are f same datatype in the table and they will be set as ints, but not these ones........any ideas?
PHP Code:
<?
(!defined('')) ? die : '';
(!isset($_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
if (!@isset($_GET['upgrading']))
{
$user_id=$_SESSION['user_id'];
$tresult= mysql_query("SELECT * FROM `user_training`
WHERE user_id = $user_id");
if ($train=mysql_fetch_assoc($tresult)){
$cost=0;
is_int($cost);
if (is_int($cost)){echo' cost is an int 11 ';}else{echo'cost isnt an int 11';}
$cost=$train["production_cost"];
is_string($cost);
if (is_string($cost)){echo' cost is string ';}else{echo'cost isnt astring';}
is_int($cost);
if (is_int($cost)){echo' cost is an int';}else{echo'cost isnt an int';}
echo " produciton cost gold is $cost /";print_r($cost);echo' / ';settype($cost, "int");
}else{
echo 'cant set user gfold';
}
$uuser_gold=intval($user_gold);
is_int($uuser_gold);if(is_int($uuser_gold)){echo'uuuuser gold is an int';}else{echo'uuuser gold isnt an int';}
echo' ++ ';print_r($uuser_gold);echo" +$uuser_gold+ ";
if ($tresult)
{
echo 'tresult successful';
}else{
echo 'tresult failed';
}
$result = mysql_query("SELECT * FROM `users`
WHERE user_id = '".$user_id."'");
if ($user=mysql_fetch_assoc($result)){
$user_gold=0;
is_int($user_gold);
if (is_int($user_gold)){echo' user gold is an int 1122 ';}else{echo'user gold isnt an int 1122';}
$user_gold=$user["user_gold"];
is_string($user_gold);
if (is_string($user_gold)){echo' user gold is string ';}else{echo'user gold isnt astring';}
is_int($user_gold);
if (is_int($user_gold)){echo' gold is an int';}else{echo'gold isnt an int';}
echo " user gold is $user_gold / ";print_r($user_gold);echo' / ';settype($user_gold, "int");
}else{
echo 'cant set user gfold';
}
if ($result)
{
echo 'result successful';
}else{
echo 'result failed';
}
echo '<table width="90%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left"><b>Unit Production</b></td>
<td align="left"><b>Next Level</b></td>
<td align="left"><b>Cost</b></td>
</tr>';
while ($tmember = mysql_fetch_assoc($tresult))
{
echo '<tr>
<td align="left">'.$tmember['unit_production'].'</td>
<td align="left">'.$tmember['next_lvl'].'</td>
<td align="left">'.$tmember['production_cost'].'</td>
</tr>';
}
echo '</table>';
echo '
<form action="index.php?mode=training&upgrading=trueamp;" method="POST">
Upgrade: <input type="submit" value="Upgrade" />
<input type="hidden" name="Upgrade" value="true" />
</form>';
} else if (@isset($_GET['upgrading'])) {
$tresult= mysql_query("SELECT * FROM `user_training`
WHERE user_id = '".$user_id."'");
$tmember = mysql_fetch_assoc($tresult)or die('an error occured in this query:'.$tresult.'. The error is: '.mysql_error()) ;
echo' // ';;print_r($user_gold);echo' // ';
echo' // ';print_r($cost);echo' // ';
if ($user_gold>=$cost)
{
$new_user_gold=0;
$new_user_gold=$user["user_gold"]-$train["production_$cost"];
settype ($new_user_gold, "int");
is_int($new_ser_gold);
if (is_int($new_user_gold)){echo' new user gold is an int';}else{echo'new user gold isnt an int';}
echo "new gold is $new_user_gold / ";
print_r($new_user_gold);echo'/';
$nproduction_lvl=$tmember['unit_production']+1;
is_int($nproduction_lvl);
if (is_int($nproduction_lvl)){echo' $nproduciton_lvl is an int';}else{echo'$nproduciton_lvl isnt an int';}
echo " produciton cost gold is $cost /";print_r($cost);echo' / ';settype($cost, "int");
$nnext_lvl=$tmember['next_lvl']+1;
$addition=0;
$addition=$nproduction_lvl+($nproduction_lvl*82);
settype ($addition, "int");
$ncost=addition+($cost*2);
echo "new cost is $ncost / ";
is_int($ncost);
if (is_int($ncost)){echo' new cost is an int';}else{echo'new cost isnt an int';}
settype($ncost, "int");print_r(ncost); echo'/';
$user_result = mysql_query("UPDATE `users`
SET user_gold = '".$new_user_gold."'
WHERE user_id = '".$_SESSION['user_id']."'");
$tuser_result=mysql_query("UPDATE `user_training`
SET unit_production = '".$nproduction_lvl."',
next_lvl = '".$nnext_lvl."',
production_cost =$ncost
WHERE user_id = '".$_SESSION['user_id']."'");
}else{
echo 'Not enough gold';
timed_redirect('index.php?mode=training', 5);
}
if ($user_result)
{
echo 'Upgrade Successful!';
timed_redirect('index.php?mode=training', 5);
}else{
echo 'Error Occurred1';
timed_redirect('index.php?mode=training', 5);
}
if ($tuser_result)
{
echo 'Upgrade Successfu22l!';
timed_redirect('index.php?mode=training', 5);
}else{
echo 'Error Occurre2d: '.$tuser_result.'. The error is: '.mysql_error();
timed_redirect('index.php?mode=training', 5);
}
}
?>