PHP/MySQL problem from hell

PHP/Mysql webgame

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 3046 Views - Last Post: 18 March 2009 - 12:01 AM Rate Topic: -----

#16 Rikarus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-March 09

Re: PHP/MySQL problem from hell

Posted 14 March 2009 - 04:41 PM

<?php
include 'config.php';
$id = 1;
$result = mysql_query("SELECT * FROM Users");
$num_rows = mysql_num_rows($result);
do
{	
	$food	= mysql_query("SELECT food  FROM Users WHERE id = '$id'");
	$foodinc = mysql_query("SELECT chefs FROM Users WHERE id = '$id'");
	$foodadd = $food + $foodinc;
	mysql_query("UPDATE Users SET food = '$foodadd' WHERE id = '$id'");
	$id = $id+1;
}
while($id <= $num_rows);

?>



For some reason its making ID 1's food = 9, ID 2's food = 13, ID 3's food = 17, etc.

Each ID is adding 4, its the strangest thing!

Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#17 Rikarus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-March 09

Re: PHP/MySQL problem from hell

Posted 17 March 2009 - 11:57 PM

It lives!

To anyone who was curious as to how I fixed it, here is my code.

Sorry for the doublepost, I couldn't figure out how to edit my old post >_<

<?php
include 'config.php';
$id = 1;
$rowinput = mysql_query("SELECT * FROM Users");
$num_rows = mysql_num_rows($rowinput);
do
{	
	$result = mysql_query("SELECT miners, chefs, ore, food, recruits FROM Users WHERE id='$id'");
	$row = mysql_fetch_assoc($result);
	extract($row,EXTR_OVERWRITE);
	$foodadd = $food + $chefs;
	mysql_query("UPDATE Users SET food = '$foodadd' WHERE id = '$id'");
	$id++;
}
while($id <= $num_rows);

?>


Was This Post Helpful? 0
  • +
  • -

#18 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: PHP/MySQL problem from hell

Posted 18 March 2009 - 12:01 AM

If you change your query to just select food and chefs, it'll use less memory. Unless you're planning on having that section of code do something with ore, miners, etc. sometime down the road.

Apologies for not replying. I actually didn't see you'd made a new reply to this post :blink:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2