简体   繁体   中英

How do I make query faster with PHP?

I have a problem with the time to query my database with PHP. I recently started creating an MMO RPG ONLINE using the program engine001 , I also use PHP and MySQL to query info of my database but it takes too long to make a query (it usually takes one sec to query).

Steps:

  1. First of all I query a webpage of my website which has the .php file (site.com/file.php)
  2. This php file, will query into my database
  3. This is printed (echo) at the page so my game pick the value e then make whatever it wants.

Here we have an example, this PHP file will send the new position of an actor:

include("mysqlconfig.inc");

$id = $_REQUEST['id'];
$positionX = $_REQUEST['positionX'];
$positionY = $_REQUEST['positionY'];
$query = "UPDATE players SET positionX = '$positionX',positionY = '$positionY' WHERE ID = $id ";

$res = mysql_query($query);


mysql_close($con);

I have no idea how online games are made and why they are so fast but this is how I do mine. It's the only way I figured out how to do that.

My question is, is there any way to make it a little bit faster?

I don't think this approach will ever be fast enough for a real time game. As you have discovered, making database queries for things like player positions is far too slow. Real time games do not typically use web-based backends because HTTP is a stateless protocol, but you need to have the game state persist in the process's memory to make it fast enough. So you will probably have to look at writing the backend as a custom server using sockets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM