Jump to content

"Build" script


Worqy

Recommended Posts

Hi.

Those who have seen my post before knows that I'm making a MMOG game.

Now I have come to that part where I'm making a system so the player/user can build buildings.

This is the code:

<?php
// Server 1, buildprogress.php

Session_start();

include 'config.php';

date_default_timezone_set('Europe/Helsinki');

$localtime = time();
$buildtime = $_SESSION['buildtime'];

echo "Localtime: " . date("H:i:s",$localtime);
echo "<br>";
echo "Buildtime: " . $buildtime;
echo "<br>";
list($h, $m, $s) = explode(":", $buildtime);
$finishtime = date("H:i:s", strtotime("+$h hours $m minutes $s seconds", $localtime));
echo $finishtime;


// Settings
$id = $_SESSION['fieldID'];	
$myusername = $_SESSION['username'];
$villageID = $_SESSION['villageID'];
$buildtime = $_SESSION['buildtime'];

// Connect to server and select database
$connect = mysql_connect("$host","$username","$password")or die("Error when trying to connect to MySQL");
mysql_select_db("build")or die("Error when trying to select database in MySQL");

// Select field	
$sql = mysql_query("SELECT * FROM build") or die(mysql_error());


	$buildcommand = mysql_query("INSERT INTO build (username, village ID, building ID, time, ready at) VALUES ($myusername, $villageID, $id, $buildtime, $finishtime);");
	echo "Done!";

?>

 

Now, this code is not working.

No error line, it just doesn't put the values in MySQL.

I'm also working on a script that checks if the time(now) is 'time when building is ready'.

I will post in this topic again if I get some problems with it.

 

Link to comment
Share on other sites

It's not working because your INSERT INTO query is not valid syntax. You need to put quotes around all string variables that go into the query, so it should be

 

$buildcommand = mysql_query("INSERT INTO build (username, village ID, building ID, time, ready at) VALUES ('$myusername', '$villageID', '$id', '$buildtime', '$finishtime');");

Link to comment
Share on other sites

Also, when you do queries, its good practice to use an intermediary, like a function, but to make things easy here ill tell you a quick tip;

 

Mysql queries can and do go wrong so this is how i would do it first time:

-build the statement

-echo the query

-check for errors

 

To accomplish this:

 

$query = "INSERT INTO build (username, village ID, building ID, time, ready at) VALUES ('$myusername', '$villageID', '$id', '$buildtime', '$finishtime')";
$buildcommand = mysql_query($query);
if(mysql_error()){
   echo "Error Query:<br />\n".$query."<hr />".mysql_error();
}
echo "Done!";

 

-CB-

Link to comment
Share on other sites

Thank you all for your answers.

Now I tested the code but I get an error.

New code:

<?php
// Server 1, buildprogress.php

Session_start();

include 'config.php';

date_default_timezone_set('Europe/Helsinki');

$localtime = time();
$buildtime = $_SESSION['buildtime'];

echo "Localtime: " . date("H:i:s",$localtime);
echo "<br>";
echo "Buildtime: " . $buildtime;
echo "<br>";
list($h, $m, $s) = explode(":", $buildtime);
$finishtime = date("H:i:s", strtotime("+$h hours $m minutes $s seconds", $localtime));
echo $finishtime;


// Settings
$id = $_SESSION['fieldID'];	
$myusername = $_SESSION['username'];
$villageID = $_SESSION['villageID'];
$buildtime = $_SESSION['buildtime'];

// Checking if values are set
echo "<br />";
echo $myusername."<br />";
echo $villageID."<br />";	
echo $id."<br />";
echo $buildtime."<br />";
echo $finishtime."<br />";

// Connect to server and select database
$connect = mysql_connect("$host","$username","$password")or die("Error when trying to connect to MySQL");
mysql_select_db("build")or die("Error when trying to select database in MySQL");

// Select field	
$sql = mysql_query("SELECT * FROM build") or die(mysql_error());



	$query = mysql_query("INSERT INTO build (username, village ID, building ID, time, ready at) VALUES ('$myusername', '$villageID', '$id', '$buildtime', '$finishtime');");
	$buildcommand = mysql_query($query);
	if(mysql_error()){
	echo "<br />Error Query:<br />\n".$query.mysql_error()."<br />";
	}else{
	echo "Done!";
	}
?>

 

Output on screen:

Localtime: 19:30:58

Buildtime: 00:10:00

19:40:58

Admin

001

1

00:10:00

19:40:58

 

Error Query:

Query was empty

 

Whats wrong now?

 

// Kevin

Link to comment
Share on other sites

i would reccomend changing the offending line:

 

From:

$query = mysql_query("INSERT INTO build (username, village ID, building ID, time, ready at) VALUES ('$myusername', '$villageID', '$id', '$buildtime', '$finishtime');");

 

To:

$query = "INSERT INTO build (username, village ID, building ID, time, ready at) VALUES ('$myusername', '$villageID', '$id', '$buildtime', '$finishtime')";

 

-CB-

Link to comment
Share on other sites

Now I have edited the code:

<?php
Session_start();

include 'config.php';

date_default_timezone_set('Europe/Helsinki');

$localtime = time();
$buildtime = $_SESSION['buildtime'];

echo "Localtime: " . date("H:i:s",$localtime);
echo "<br>";
echo "Buildtime: " . $buildtime;
echo "<br>";
list($h, $m, $s) = explode(":", $buildtime);
$finishtime = date("H:i:s", strtotime("+$h hours $m minutes $s seconds", $localtime));
echo $finishtime;


// Settings
$id = $_SESSION['fieldID'];	
$myusername = $_SESSION['username'];
$villageID = $_SESSION['villageID'];
$buildtime = $_SESSION['buildtime'];

// Checking if values are set
echo "<br />";
echo $myusername."<br />";
echo $villageID."<br />";	
echo $id."<br />";
echo $buildtime."<br />";
echo $finishtime."<br />";

// Connect to server and select database
$connect = mysql_connect("$host","$username","$password")or die("Error when trying to connect to MySQL");
mysql_select_db("build")or die("Error when trying to select database in MySQL");

// Select field	
$sql = mysql_query("SELECT * FROM build") or die(mysql_error());



	$query =  "INSERT INTO build (username, village ID, building ID, time, ready at) VALUES ('$myusername', '$villageID', '$id', '$buildtime', '$finishtime')";
	$buildcommand = mysql_query($query);
	if(mysql_error()){
	echo "<br />Error Query:<br />\n".$query.mysql_error()."<br />";
	}else{
	echo "Done!";
	}
?>

 

Now I get this output:

Localtime: 14:48:38

Buildtime: 00:10:00

14:58:38

Admin

001

1

00:10:00

14:58:38

 

Error Query:

INSERT INTO build (username, village ID, building ID, time, ready at) VALUES ('Admin', '001', '1', '00:10:00', '14:58:38')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 'ID, building ID, time, ready at) VALUES ('Admin', '001', '1', '00:10:00', '14:58' at line 1

Link to comment
Share on other sites

Sorry for not answering for a long time, my PC broke down.

 

Thank you for your answer, it worked perfectly.

 

Now I just go one little thing.

 

Code:

<?php
// Server 1, checkbuilding

Session_start();

include 'config.php';

// Settings
$localtime = time();

// Connect to server and select database
$connect = mysql_connect("$host","$username","$password")or die("Error when trying to connect to MySQL");
mysql_select_db("build")or die("Error when trying to select database in MySQL");

$sql="SELECT * FROM build";
$result=mysql_query($sql);
if(mysql_error()){
	echo "<br /><b>Error Query:</b><br />\n".$query.mysql_error()."<br />";
	}else{
	echo "<br /><b>Error Query:</b><br />";
	echo "No errors found!";
	}
while($data = mysql_fetch_array( $sql ))
{
if($localtime == $data['readyat'])
{
echo "DONE!";
}else{
echo "FAIL!";
} 

}		

 

Output:

Error Query:

No errors found!

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\Game\S1\checkbuilding.php on line 23

 

MySQL table values:

Admin  001  1 00:10:00  20:55:36

 

Now I would like my script to (1) work and (2) loop throught all the rows in MySQL and look in everyone of them if 'readyat' = localtime and then echo out the 'buildingID' for the matching row.

 

// Kevin

 

Link to comment
Share on other sites

Thank you. Got everything to work now :)

 

// Kevin

 

EDIT:

Just one last question:

 

Let say I create a database named dbtest and i table in it named tbltest. Now In this table i but to values: Name and Age.

Then I fill in two rows:

Row 1: Kevin 85

Row 2: Peter 75

 

For me this is a bit hard to explain, but If I would have one rown I would use this code:

("SELECT * FROM buildings WHERE Name=Kevin")

and then I would echo the value.

But now I have two rows and two names, can I just do the SQL statement again.

example:

("SELECT * FROM buildings WHERE Name=Kevin")

and then a bit later:

("SELECT * FROM buildings WHERE Name=Peter")

or do I have to disconnect to MySQL before?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.