0

I want $userhp1 to print to screen, and also save to the allocated sql table. This works fine, but they are both returning different results. Is there anyway to make them return the same value?

ob_start();
vitality(); //runs vitality function
$uservit = ob_get_contents(); // stores result on $uservit
ob_end_clean();
echo "-Vitality:"; 
echo "$uservit<br />";// Prints vitality
$userhp1 = $uservit * 2.8; // multiplys vitality too create HP
$userhp2 = $userhp1;

echo "-HP From vit:";
echo "$userhp2"; // prints hp

$con=mysqli_connect("localhost", "***", "***", "***");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"UPDATE users SET hp=$userhp2 
WHERE id='1'");
mysqli_close($con);
5
  • 1
    "But they are both returning different results" - what is returning different results? Commented Apr 15, 2013 at 12:58
  • $userhp1 is enough to be used, you don't need another var Commented Apr 15, 2013 at 12:58
  • what do you mean by different results .can you give 1 example .?/ Commented Apr 15, 2013 at 12:59
  • You should stop using the depreciated mysql extension in php. Use mysqli or PDO instead. And you should make sure that $userhp2 really contains a numerical value... Commented Apr 15, 2013 at 13:02
  • sorry guys i suck at explaining stuff. the vitality() function rolls a number between 1-100. $userhp multiplys it by 2.8, Then that number is displayed in my game as HP bonus from a item. at the same time that number from $userhp should get saved to that table. But it saves a different number then which is displayed. Like it does the whole command again when $userhp is called upon in the sql statement. Commented Apr 15, 2013 at 13:07

1 Answer 1

1

There are two things wrong in your code:

ob_start();
vitality();
$uservit = ob_get_contents();

Here you seem to be using the ob_* function family to return a value from a function. ob_get_contents will return a string and you don't seem pretty happy about it since you are using it as an integer in:

$userhp1 = $uservit * 2.8;

You can just return the value from the function. Output buffering is used for other things.

Second, you are declaring $userhp2 = $userhp1; and then you just don't modify neither $userhp1 nor $userhp2. Why don't you just use $userhp1 over $userhp2 and never set the latter in the first place?

On a side note: mysql_* functions are in the process of being removed and they have been deprecated. Use PDO or mysqli instead.

Sign up to request clarification or add additional context in comments.

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.