Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Worqy
Forum Newbie
Posts: 20
Joined: Tue Feb 02, 2010 6:41 am

Error

Post by Worqy »

This is my code:

Code: Select all

<?php
// Server 1, auto-add.php
// Start Session
Session_Start();

// Include
include "config.php";

// Settings
	$villageId = "001";

// Loop
while($villageId < 100)
{

	// Connect to MySQL and select database
	$connect = mysql_connect("$host", "$username", "$password")or die("cannot connect");
	// Select database
	mysql_select_db("production")or die("cannot select Database");
	//Include all the maths of iron mine levels etc...

	$data = mysql_query("SELECT * FROM production WHERE villageID=$villageId")
	or die(mysql_error());  
	while($info = mysql_fetch_array( $data ))
	{
			// GOLD
		// $Prod* = Production of resources every 60min 
		$ProdGold = $info['gold'];
		// Math
		// $Prod* / 3600seconds (60*60)
		$ProdGold = $ProdGold / 3600;
			// THREE
		// $Prod* = Production of resources every 60min 
		$ProdThree = $info['three'];
		// Math
		// $Prod* / 3600seconds (60*60)
		$ProdThree = $ProdThree / 3600;
			// CLAY
		// $Prod* = Production of resources every 60min 
		$ProdClay = $info['clay'];
		// Math
		// $Prod* / 3600seconds (60*60)
		$ProdClay = $ProdClay / 3600;
			// IRON
		// $Prod* = Production of resources every 60min 
		$ProdIron = $info['iron'];
		// Math
		// $Prod* / 3600seconds (60*60)
		$ProdIron = $ProdIron / 3600;
				// WHEAT
		// $Prod* = Production of resources every 60min 
		$ProdWheat = $info['wheat'];
		// Math
		// $Prod* / 3600seconds (60*60)
		$ProdWheat = $ProdWheat / 3600;
			
			// MySQL close connection
			mysql_close($connect);
			
			// Connect to MySQL and select database
			$connect1 = mysql_connect("$host", "$username", "$password")or die("cannot connect");
			// Select database
			mysql_select_db("resources")or die("cannot select Database");

			// Gold
			// Updates MySQL
			mysql_query("UPDATE resources WHERE villageID=$villageId SET gold = gold+$ProdGold");

			// Three
			// Updates MySQL
			mysql_query("UPDATE resources WHERE villageID=$villageId SET three = three+$ProdThree");

			// Clay
			// Updates MySQL
			mysql_query("UPDATE resources WHERE villageID=$villageId SET clay = clay+$ProdClay");

			// Iron
			// Updates MySQL
			mysql_query("UPDATE resources WHERE villageID=$villageId SET iron = iron+$ProdIron");

			// Wheat
			// Updates MySQL
			mysql_query("UPDATE resources WHERE villageID=$villageId SET wheat = wheat+$ProdWheat");
		
	
	}
	
	// Next village
	$villageId ++;
	echo mysql_error();
}
?>

<html>
<head>  
	<!-- Meta refresh every second -->
	<meta http-equiv="refresh" content="1">
</head>
<body>
</body>
</html>
When I run it, I get this error:
[text]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE villageID=001 SET wheat = wheat+1.1111111111111' at line 1[/text](two times after each other )

And this is how my MySQL looks like:
[text]Koko tekstit username villageID gold three clay iron wheat
Muokkaa Poista Admin 001 0 0 0 0 0
Muokkaa Poista Admin 002 0 0 0 0 0[/text] ( In finnish :P )
So I really can't see the problem...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Error

Post by Christopher »

You need to move the SET to before the WHRERE. The syntax is "UPDATE SET WHERE".

Also, you should only have one mysql_connect() and it should be before your loop. You only need mysql_select_db() inside the loop.
(#10850)
Post Reply