I only recently learned how to use MooSock properly myself - so I thought I'd share some of my knowledge with the people who haven't used it much before..

If you haven't a clue how to use moosock, or don't know what it is, I recommend you read this article by Tigerworks first: http://www.create-games.com/article.asp?id=1405

What is this article about?
Basically - this article will teach you how to send information from an MMF application, to a MySQL database. If you haven't a clue what MySQL is, this will be useless to you. Using MooSock and MySQL together, along with PHP, is actually very useful - especially for creating things like online score tables or maybe a guestbook type thing.

What you're gonna need
Obviously, you'll need MMF 1.5 and the MooSock extension (www.3ee.com). You will also need a web host, which has MySQL with it - you usually need to buy this, however there are some free hosts which offer this - the one I use is www.viperhosting.net - however, this host is only kept up by donations from the public, and so it could go down at any time..

You will probably need some basic PHP knowledge - although the PHP scripts you need will be posted here.

What we will make
To start of easily, we'll make a simple application. The user of the application will type his/her name into an edit box, and click a button - the name they entered will then be sent and inserted into a MySQL table. There will then be a page that shows all the names that have been submitted.

Step One - The Application
Firstly, we'll start off by making the application. You're going to need the following objects on the playfield:

- An edit object (call this Name)
- A button (call this Submit)
- String Object (call this Message)
- MooSock Extension

Image

Yup - that's all you need! The events we will use are very straight forward, and there arn't actually that many events at all!

Firstly, we need to connect to your server/host. I am registered to www.viperhosting.net (above) - and my web address is www.flava.viperhosting.net - so I will connect to that:

SUBMIT BUTTON - Is Clicked
+ SUBMIT BUTTON - Disable
+ MOOSOCK - Connect to www.flava.viperhosting.net on port 80


Image

Firstly, we disable the submit button so that the user cannot keep clicking it - as this will flood your MySQL table with lots of names. We then connect to my host, www.flava.viperhosting.net. You obviously change this to your address - also see how there is no http:// at the start of it - thats because you dont need it. We then connect to port 80, which is standard for HTTP addresses.

MOOSOCK - On Connect
+ MOOSOCK - Send text line POST + http://www.flava.viperhosting.net/addname.php?name= + Edittext$( Name ) + HTTP/1.0
+ Send text line From: flava@viperhosting.net
+ Send text line User-Agent: HTTPTool/1.0
+ Send text line
+ Disconnect


Image

This is the slightly confusing bit. Firstly, we'll start with the Send text line POST + http://www.flava.viperhosting.net/addname.php?name= + Edittext$( Name ) + HTTP/1.0 part. The POST part basically tells the client to post the information into our PHP page. Next, we have http://www.flava.viperhosting.net/addname.php?name= + Edittext$( Name ). The PHP page is called addname.php - we'll make this later. Adding ?name=NAME GOES HERE onto the end, will set the $name variable to the name specified - in this case, the name specified is from the Edit Object. We then post it under HTTP/1.0.

WARNING: You MUST put a space after POST and before HTTP/1.0 - so they should be POST and HTTP/1.0 - I didn't put spaces, and it took me ages to figure out why it wasnt working!

Our next text line is From: flava@viperhosting.net - this isn't too important to know. In fact, i'm not 100% sure what it is for - but it is needed. I simply put my host username in this part, and it works fine. The User-Agent: HTTPTool/1.0 part is also important - make sure you have it. You MUST also send an empty text line (Send text line ) - this ends the function so to say. We then disconnect from the server, as we no longer need to connect. This whole event sends the score to the addname.php page.

MOOSOCK - On Disconnect
+ STRING - Change alterable string to Name submitted


As the send name event ends with Disconnect, we can tell the application to post a Name Submitted comment when the host/sever has disconnected.

Phew. The hard part over with - the whole application is completely finished now! Now we need to sort out the addname.php, viewnames.php pages and the MySQL tables.


Step Two - MySQL Database and Table
For this part, I am using something called PhpMyAdmin which came free with my host - it's a small browser that lets me add databases and tables to my MySQL host thing - however, you can add the databases and tables however you like. (PhpMyAdmin is usually available with Cpanel which comes with most hosts)

Firstly, create a database in MySQL - I've called mine flava_test and so will use this example throughout the article. Select the database you just created, and create a new table in it - call this table names and give it 2 fields.

Call the first field id and change the field type to INT or INTEGER - change it to auto_increment (under the Extra section on PhpMyAdmin) and make it the Primary Key. All names submitted will be given an ID Number that nobody else has.

Call the second field name - this will be were all names are stored. Change the field type to VARCHAR and specify a value length of your choice. Save the table.

Image


Step Three - addname.php page
When the submit button is clicked, the name specified will be sent to the addname.php page. This page will recieve the name and post it onto your table.

Type in this code into notepad and save as addname.php:

Image

You will need to change the information in this code, so that it connects to YOUR database. I have obviously blanked out my password. Change the flava part to your host username, and enter your password to your host.

If you remember, we added ?name=NAME GOES HERE onto the end of our address on our application. Firstly, we must tell the PHP page to GET or retrieve this information.

The $name = $_GET['name']; retrives the name from our application so that it can be used in any way that we like - in this case, we will store it in our database.

Change the flava_test part to the name of your database. The insert into names part, tells the page that we will inset the information into the table called names.

That is basically it for our addname.php page - any problems, feel free to PM me and I'll sort it out


Step Four - viewname.php page
The viewname.php page will simply view all the names that have been submitted from your application. This is simply just by using PHP and MySQL together.

Image

This is the code for the page. Again, insert your host and MySQL information like you did with addpage.php.

The $sql=select id, name from names order by ID ASC; part tells the page to select id and name from the NAMES table and order it by the id in ASCENDING ORDER.

We can then echo $row[id] to display the ID numbers, and $row[name] to display the names, in a WHILE LOOP so that all the IDs and Names are displayed. Again, any problems with this - PM me


Finished!
Try submitting a name in your application, and visit your viewname.php page to see if the name appears. If not, check out the example files I have provided below and see where you may have gone wrong!


Where next?
With this method of posting information into a MySQL database, you can create a simple scoreboard system.

For example, you can POST http://yoursite.com/addscore?name=Flava&score=SCORE-HERE - and have a score and name field in your database.

You will then need to add the score variable to parts of your PHP script - just put the word score where it says name on the script (if you have any problems with this, dont hesitate to message me).

Example Files
I know how fustrating it is to get slightly excited about making something from an article, and then for it not to work. I have reason to believe that this method may not work on some browsers - but as far as I am aware, it works on most well known browsers such as IE and Mozilla.

I have provided a .zip file whic contains addname.php, viewname.php and moosocktut.cca - feel free to look through them:

Example Files: http://www.ectoprods.com/flava/moosock/MooSockTut.zip (6.15kb)

Hope this article was helpful to some of you - if you spot any mistakes, or have problems with getting it to work, post in the comments part or give me a PM