Worqy Posted April 11, 2010 Share Posted April 11, 2010 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. Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 11, 2010 Share Posted April 11, 2010 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');"); Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted April 11, 2010 Share Posted April 11, 2010 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- Quote Link to comment Share on other sites More sharing options...
Worqy Posted April 12, 2010 Author Share Posted April 12, 2010 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 Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 12, 2010 Share Posted April 12, 2010 You're calling mysql_query with the result of another mysql_query call. Need to remove $buildcommand = mysql_query($query); Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted April 13, 2010 Share Posted April 13, 2010 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- Quote Link to comment Share on other sites More sharing options...
Worqy Posted April 13, 2010 Author Share Posted April 13, 2010 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 Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted April 13, 2010 Share Posted April 13, 2010 Tip, Do not use spaces in any mysql names (columns/tables etc). Its makes life impossible. For now thoguh, wrap each Column name in back quotes: ` eg: INSERT INTO build (`username`, `village ID`... but leave the values. -CB- Quote Link to comment Share on other sites More sharing options...
Worqy Posted April 16, 2010 Author Share Posted April 16, 2010 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 Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted April 16, 2010 Share Posted April 16, 2010 The error, read and understand, most are quite lf explanatory like this one: Resource expected, given string, on line 25. $sql is a string. $result is the resource you need. hint while($data = mysql_fetch_array( $sql )) -cb- Quote Link to comment Share on other sites More sharing options...
Worqy Posted April 17, 2010 Author Share Posted April 17, 2010 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? Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted April 17, 2010 Share Posted April 17, 2010 No, if you want to display both results, use a while loop. There are plenty of tutorials on this site and on the web for this. http://www.lmgtfy.com/?q=PHP+MYSQL+TUTORIAL -cb- Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.