Page 1 of 2

Problem whit retrieving data from MySQL

Posted: Tue Mar 09, 2010 10:22 am
by Worqy
Hello.
I'm working on a MMOG, just for fun.
Now I need to get some data form MySQL, the resources for the 'villages'(in the game)
I think I have found the problem.
This is what I want the code to do:
*Get data from MySQL(checkstatus.php)
*Store it in a Session(checkstatus.php)
*Print it out(main.php)
The codes:
checkstatus.php

Code: Select all

 
<?php
// Server 1, checklogin.php
Session_start();
 
include "connect.config.php";
 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select Database");
 
$data = mysql_query("SELECT * FROM `resoreces`")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Username:</th> <td>".$info['username'] . "</td>";
Print "</tr><tr>";
Print "<th>Gold:</th> <td>".$info['gold'] . "</td>"; // Forget this, just something I tested
Print "</tr><tr>";
Print "<th>Three:</th> <td>".$info['three'] . "</td> ";
Print "</tr><tr>";
Print "<th>Clay:</th> <td>".$info['clay'] . "</td> ";
Print "</tr><tr>";
Print "<th>Iron:</th> <td>".$info['iron'] . "</td> ";
Print "</tr><tr>";
Print "<th>Wheat:</th> <td>".$info['wheat'] . " </td>";
Print "</tr>"; // Now the session thing happends...
$_SESSION['Three'] = $info['three']; //<- Problem ( I think )
}
?>
<html>
<body>
<a href="main.php">Back</a>
</body>
</html>
<?php
Print "</table>";
?> 
 
main.php

Code: Select all

<?php
// Server 1, main.php
Session_start();
 
if($_SESSION['LoginS1'] == false) 
{ 
?>
<html>
<head>
    <title>Access denied!</title>
</head>
<body>
<center>Access denied!</center>
<p>
Please login to view this site!
<p>
<a href="/Game/login.php">Login</a>
</body>
</html> 
<?php
}
else
{
?>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
<center>
<?php
echo 'Three: ';
echo $_SESSION['Three'];
 
?>
</center>
<p>
<center><a href="checkstatus.php">Check</a></center>
<a href="/Game/logout.php">Logout</a>
</body>
</html>
<?php
}
?>
The problem is (I think) is that the value 'three' is nothing...
So I think there is something wrong with this:

Code: Select all

$_SESSION['Three'] = $info['three'];
The problem might be simple, but I'm still learning PHP and MySQL.
Thank you for your time so far!
//Kevin

Re: Problem whit retrieving data from MySQL

Posted: Tue Mar 09, 2010 12:07 pm
by Kurby
$data = mysql_query("SELECT * FROM `resoreces`")
Did you spell resources right?
$_SESSION['Three'] = $info['three'];
This is in a loop. At the end of the loop $_SESSION['Three'] will only contain the three cell from the last row of data.

Re: Problem whit retrieving data from MySQL

Posted: Tue Mar 09, 2010 1:02 pm
by Worqy
I got some help from one of my friends, so I solved the problem.
Now I just need one thing.
I dont know if this is possible in PHP, but how can I "update" my resources like every second?
Like now when I press a link and go to checkstatus.php and go back to main.php my resources have been updated, but I would need to do this every second...

Re: Problem whit retrieving data from MySQL

Posted: Wed Mar 10, 2010 9:37 am
by Worqy
*Waiting for a reply*

In other words:
I need a "engine" that adds a value to the MySQL variables for example ever second.
Like:

Code: Select all

 
//All the retrieving stuff...
//Loop maby?
$info['three'] + "1";
 
And I need this "engine" to run all the time.

Re: Problem whit retrieving data from MySQL

Posted: Wed Mar 10, 2010 10:24 am
by Grizzzzzzzzzz
sounds like a Cron job

Re: Problem whit retrieving data from MySQL

Posted: Thu Mar 11, 2010 7:17 am
by Worqy
Okey 8O
Is this something you can easy "do" with PHP?
Like make a script that does that or something?
Its better that I explain everything so you will understand:
I'm making a MMOG, and in the game I will have resources.
Now you can update your "resource factories" (Like: Production 60 Iron/Hour)
So I need my "engine" to add (in this case with 60iron/h) 1 "iron" to MySQL every minute.
If the production is 100 , I want it to add 1 (The real number is 1,4 , so the first minute 1, second minute 1 and third minute 2, 4th minute 1, 5th minute 2... So if will be 100 in one hour)
Hope somebody understand what I mean!
//Kevin

Re: Problem whit retrieving data from MySQL

Posted: Thu Mar 11, 2010 7:44 am
by Grizzzzzzzzzz
:| the easiest way i can imagine to do it, would be to write a script that does the update, and has an automatic meta-refresh in it that occurs every hour or however long you want.

Code: Select all

 
<?php
    mysql_connect("localhost", "root", "test");
    mysql_select_db("test");
    
    mysql_query("UPDATE iron SET iron = iron+60");
    
    echo "done!";
?>
<!--add more iron every 5 seconds-->
<meta http-equiv="refresh" content="5">
 
Access that script from a computer and just leave the page open :roll:

Obviously that is a massive security risk, and has thousands of complications and downsides associated to it. Although.....if you are doing it just for fun, that might be enough.


if you want to do something like you mentioned properly however, there isn't a quick & easy way to do it. At least not that i know of off the top of my head. If anybody knows of anything then spill!


Like i said previously, you'll need a serverside application to schedule and run it for you, like Cron or something.

Re: Problem whit retrieving data from MySQL

Posted: Thu Mar 11, 2010 8:17 am
by Worqy
hmm...
I'm very noob to PHP.
I have heard about META, but I dont know how it works?

And your way could work, but how is it then with many users and I need the script to update lets say 100 "resources" every sec?

Re: Problem whit retrieving data from MySQL

Posted: Thu Mar 11, 2010 8:51 am
by Grizzzzzzzzzz
Worqy wrote:hmm...
I'm very noob to PHP.
I have heard about META, but I dont know how it works?

And your way could work, but how is it then with many users and I need the script to update lets say 100 "resources" every sec?
the META refresh tag is just a piece of HTML, it just refreshs the page, and hence the PHP script is run again. People usually use it for redirects, you'll find it on those pages which say "redirecting you in 10seconds". But it also works fine in this example.



it depends how complex your game is going to be, and how many users are going to play it.

If it's just a simple case of every user gets 100 resources every second, you'd probably run into no problems.

But, if you had other factors altering this value, for example users could build iron mines increasing their personal production, then alot of maths will start taking place and the script will take more time to complete. At an estimate it could probably handle about 50-300players, entirely depending on how complex things got.


I would never recommend the method i've stated above to be used in a public game, there is far too much that could go wrong.


I'd only say it was viable if it was a private game, played between you and people you know.



It's a very, very dirty and incorrect way of doing things, but it is the easiest i can think of. I'd wait around and see if anyone else has any other ideas.

Re: Problem whit retrieving data from MySQL

Posted: Thu Mar 11, 2010 11:05 am
by Worqy
Ok
As I said in post before, there will be math, because the user can upgrade the iron mine.
But I think the math will be quite simple:
1200iron/hour
1200/60= 20
1600/60=26,66
(I got some tips from a friend: Insted of giving out 2res/min (See my post Thu Mar 11, 2010 3:17 pm) I would give resources every second. Example:
1200Iron/hour = 20Iron/Minute = 0,33Iron/Second:
Second: "Output iron": Iron left:
---1--------0--------(0,33iron)
---2--------0--------(0,66iron)
---3--------0--------(0,99iron)
---4--------1--------(0,32iron)
---5--------0--------(0,65iron)
....
)
But I'll wait and look for any other ideas!
Thank you for all your help so far!
//Kevin

Re: Problem whit retrieving data from MySQL

Posted: Fri Mar 12, 2010 12:19 pm
by Worqy
No one with a idea?

Re: Problem whit retrieving data from MySQL

Posted: Sat Mar 13, 2010 4:29 am
by Worqy
Anyone that has played any other MMOG where you have resources (or something like that) that uses this system? (That you get more res every minute/hour...)
My brother told me a few, haven't played them myself, but I guess they are using this system:
*Travian
*TribalWars (correct name?)

Still waiting for a idea!
//Kevin

Re: Problem whit retrieving data from MySQL

Posted: Wed Mar 17, 2010 10:38 am
by Worqy
sorry for still bothering you with my questions.
Now I have maby found what I'm looking for.
But Its JavaScript (I think).
But would it still work in PHP?

Code: Select all

 
function callbackfunc(){
// retrieve your info here
}
setInterval (callbackfunc(), 5000); // this will call callbackfunc() each 5 sec
 
edited:

Code: Select all

 
function callbackfunc(){
//retrieve MySQl values
//MySQL value + something
//add the new resource value to MySQL
}
setInterval (callbackfunc(), 5000); // this will call callbackfunc() each 5 sec
 

Re: Problem whit retrieving data from MySQL

Posted: Wed Mar 17, 2010 10:52 am
by Grizzzzzzzzzz
If you do it in javascript, they'll only gain resources whilst they have the page open. This is why i suggested a cron job for it as that'll run autonomous.

I've played OGame in the past

http://ogame.wikia.com/wiki/Resources

which has the same kind of system backing it, and they'll use an automated script or app to do it for.



Apparantly (considering nobody else has replied), the PHP option i mentioned is your best bet :x

Re: Problem whit retrieving data from MySQL

Posted: Wed Mar 17, 2010 11:12 am
by Worqy
Okey...
You mean your META idea?
Can you show me an example how it would be written? Because I really dont know so much about META.

EDIT:
Now I read about META, but I still wonder how this would work.
Because I found this piece of code:

Code: Select all

 
<meta http-equiv="refresh" content="5; url=http://www.something.com/S1/[b]checkstatus.php[/b]" />
 
This will go to the site checkstatus.php every 5 second, but now I need to use META in chechstatus.php too, so it will direct me back to main.php.
Can this be done without the user seeing the content of checkstatus.php?

EDIT^2:
Now I have moved the MySQL retrieve code form checkstatus.php to main.php,
and I use META to refresh the page every second.
This works fine, just looks a bit 'stupid' when the page refreshes every second.
So, my question is:
Is it better that I have the MySQL retrieve system in main.php, and that I make my "add resource every second" thing in main.php too?

Or shall I have the MySQL retrieve thing and the resource adding in checkstatus.php ?

And is there a way to (with META) direct the page to checkstatus.php and back to main.php, so fast that the user cant see the checklogin.php page? Because its quite disturbing for the user to be directed to a page every second :)