How to create a online multiplayer games with mysql support using php ?

In summary: Then you should create a table to store your user events. Here, for example, we'll store user messages: Table 2: Messages ID | FromUser | ToUser | Message | HasBeenRead ------------------------------------------------ 1 | 1 | 2 | Hey there | False 2 | 2 | 1 | Hello | True The Column "hasbeenread" is used to identify which messages users have received. In the above table i received your "Hello" and sent you my "Hey There"
  • #1
ngkamsengpeter
195
0
I want to create a online multiplayer games with php but I don't know how to let the members know their opponents' move realtime ?
Please help me !Thanks
 
Technology news on Phys.org
  • #2
events or messages?
 
  • #3
It won't be very efficient using PHP alone. You should have something on the client side like Javascript, Java, or, ideally, Flash. Flash and Java have socket support which is what you want, to avoid refreshing the page or querying the server every so often.
Javascript doesn't support sockets, but with AJAX you can contact the server without refreshing the page, even if you'll probably find yourself querying the server on some time interval, such as every 4 seconds.
 
  • #4
-Job- said:
It won't be very efficient using PHP alone. You should have something on the client side like Javascript, Java, or, ideally, Flash. Flash and Java have socket support which is what you want, to avoid refreshing the page or querying the server every so often.
Javascript doesn't support sockets, but with AJAX you can contact the server without refreshing the page, even if you'll probably find yourself querying the server on some time interval, such as every 4 seconds.
I will use javascript and ajax also but I don't know hot to show the opponents' move to the user . For example , I create a multiplayer online chess, and how can I show the move of the opponent to the player ? Please provide me some source code .Thanks
 
  • #5
store the event of the move int a message queue.
 
  • #6
neurocomp2003 said:
store the event of the move int a message queue.
how ? please provide me the source code or the tutorial link so that I can learn .
 
  • #7
There's three phases to what you want to accomplish:
1. Set up the schema in MySQL:
This means creating whatever tables you'll need to store your information. You should have a table for your users, an example is:
Code:
Table 1: UserInformation
ID | UserName        | FirstName | LastName | ... etc
-------------------------------------------
1  | Job             | Bobby     |  ...       etc
2  | ngkamsengpeter  | ...       |  ...       etc

Then you should create a table to store your user events. Here, for example, we'll store user messages:
Code:
Table 2: Messages
ID | FromUser | ToUser | Message   | HasBeenRead
------------------------------------------------
1  | 1        | 2      | Hey there | False
2  | 2        | 1      | Hello     | True

The Column "hasbeenread" is used to identify which messages users have received. In the above table i received your "Hello" and sent you my "Hey There" which you are yet to receive.

2. Set up the server side scripts (in your case PHP)
login.php - You should have a login script which authenticates a user and sets a session variable around with his/her username.
getmessages.php - This script retrieves any unread messages for the logged in user. It does this by using the following SQL statement:
SELECT t2.UserName, t1.Message FROM Messages AS t1
JOIN UserInformation AS t2
ON (t2.FromUser = t1.ID)
WHERE t2.ToUser = <Logged in user's id here> AND t1.HasBeenRead = False
Finally getmessages.php prints the messages on the page with a format that enables javascript to easily parse (you could use XML, but for simple information exchange such as what we're dealing with it's a lot more efficient to use some other format, such as <fromuser>~!~<message>***<fromuser>~!~<message> ... etc)

3. Set up the page that will display the new messages as they come.
This page will make use of Javascript and AJAX to load the getmessages.php script. Basically you need to setup XMLHTTP (the heart of AJAX) then create a function "GetMessages()" that uses your XMLHTTP object to load the getmessages.php script, parse its contents and present the messages to the user in a neat way.
Now you can call your GetMessages() function whenever you want to get the latest messages. You can for example use javascript's SetInterval to check for new messages every X miliseconds.
 
Last edited:
  • #8
-Job- said:
There's three phases to what you want to accomplish:
1. Set up the schema in MySQL:
This means creating whatever tables you'll need to store your information. You should have a table for your users, an example is:
Code:
Table 1: UserInformation
ID | UserName        | FirstName | LastName | ... etc
-------------------------------------------
1  | Job             | Bobby     |  ...       etc
2  | ngkamsengpeter  | ...       |  ...       etc

Then you should create a table to store your user events. Here, for example, we'll store user messages:
Code:
Table 2: Messages
ID | FromUser | ToUser | Message   | HasBeenRead
------------------------------------------------
1  | 1        | 2      | Hey there | False
2  | 2        | 1      | Hello     | True

The Column "hasbeenread" is used to identify which messages users have received. In the above table i received your "Hello" and sent you my "Hey There" which you are yet to receive.

2. Set up the server side scripts (in your case PHP)
login.php - You should have a login script which authenticates a user and sets a session variable around with his/her username.
getmessages.php - This script retrieves any unread messages for the logged in user. It does this by using the following SQL statement:

Finally getmessages.php prints the messages on the page with a format that enables javascript to easily parse (you could use XML, but for simple information exchange such as what we're dealing with it's a lot more efficient to use some other format, such as <fromuser>~!~<message>***<fromuser>~!~<message> ... etc)

3. Set up the page that will display the new messages as they come.
This page will make use of Javascript and AJAX to load the getmessages.php script. Basically you need to setup XMLHTTP (the heart of AJAX) then create a function "GetMessages()" that uses your XMLHTTP object to load the getmessages.php script, parse its contents and present the messages to the user in a neat way.
Now you can call your GetMessages() function whenever you want to get the latest messages. You can for example use javascript's SetInterval to check for new messages every X miliseconds.

That means that I want to store every player moves in the sql database and retrieve it .If my games is very long , that will use a lot of sql database space . Is it any other way of doing this ?Or can you suggest other programming language which is more suitable ?
Besides , if I want to create a 3D online games , what programming language should I use . Can I use Maya ?
 
  • #9
Naturally you can delete the read messages as they are read. The biggest problem is not the database size but the traffic the game will generate through the AJAX calls. For making web based games you should use Java, Flash or Director. Director ihas a browser plugin from Macromedia (now Adobe) just like Flash but much more powerful (it supports 3D graphics).
But in my opinion, if you want a 3D online multiplayer game, then you should drop the browser and just code it from the ground up. This way you can use much more powerful programs like 3DSMAX. The disadvantage is that you'll need to code both a game server and a game client (which is a lot of fun nonetheless).
 
  • #10
-Job- said:
Naturally you can delete the read messages as they are read. The biggest problem is not the database size but the traffic the game will generate through the AJAX calls. For making web based games you should use Java, Flash or Director. Director ihas a browser plugin from Macromedia (now Adobe) just like Flash but much more powerful (it supports 3D graphics).
But in my opinion, if you want a 3D online multiplayer game, then you should drop the browser and just code it from the ground up. This way you can use much more powerful programs like 3DSMAX. The disadvantage is that you'll need to code both a game server and a game client (which is a lot of fun nonetheless).
Then , how to code the game server and game client ?
 
  • #11
You would code your game server & client the same way you would any other application. You pick a port number for your server/client communication. For the server you build an app that listens on a port for connections. For the client you build an app that connects to your server.
The server has to keep track of connections and route/broadcast messages to users. The key step is to have all this network communication in place with a nice interface so that your game isn't concerned with the underlying communication details. In Java, or any other Object Oriented language, i would build a class "CommManager" that exposes an interface with methods such as "Connect", "Send Message" etc. Finally you would build the game on top of this framework.
I'm not really the best person to tell you about 3D graphics, I've used 3DSMax in the past only superficially.
 
Last edited:

1. How do I set up a MySQL database for my online multiplayer game?

In order to set up a MySQL database for your online multiplayer game, you will first need to have access to a MySQL server. You can either set up your own server or use a hosting service. Once you have access to a server, you can use a tool such as phpMyAdmin to create a new database and set up your tables and fields.

2. What is the role of PHP in creating an online multiplayer game with MySQL support?

PHP is a server-side scripting language that is commonly used in web development. In the context of creating an online multiplayer game with MySQL support, PHP is used to handle the server-side logic and communicate with the MySQL database. This includes tasks such as user authentication, storing and retrieving game data, and handling multiplayer interactions.

3. How can I ensure the security of my MySQL database for my online multiplayer game?

To ensure the security of your MySQL database for your online multiplayer game, there are a few steps you can take. First, make sure to use secure passwords for your database and regularly update them. Additionally, you can use encryption methods to protect sensitive data such as user passwords. It is also important to regularly back up your database to prevent data loss.

4. What are some key considerations when designing a database for an online multiplayer game?

When designing a database for an online multiplayer game, there are a few key considerations to keep in mind. First, determine the types of data that will need to be stored, such as user profiles, game data, and chat logs. Then, plan out the structure of your tables and fields to efficiently store and retrieve this data. It is also important to consider scalability and potential future updates to your game.

5. Can I use other databases instead of MySQL for my online multiplayer game with PHP?

Yes, you can use other databases instead of MySQL for your online multiplayer game with PHP. Some other popular options include PostgreSQL, MongoDB, and SQLite. However, keep in mind that the specific functions and syntax may vary depending on the database you choose, so you may need to make adjustments to your code accordingly.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
2
Views
25K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
7
Views
6K
Replies
6
Views
6K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
5K
Back
Top