PHP & MySQL SL Development Manual v2.0
PHP & MySQL SL Development Manual v2.0
SL
Development
Manual
version 2.0
No part of this manual may be reproduced, stored in a retrieval system or transmitted in any form
or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise.
1 / 30
Understand Database
Any database will be stored in your MySQL server which is stored in your web host server. A
database consists of a structure to hold the data. Each of them can exist without any data and
waiting for data to be stored in it.
Similar as what you used to as spreadsheet, data in a database is stored and organized in table
format (a sheet). It is required to create the table before you can store anything (make a new
spreadsheet file). First you create the empty database, then add empty table to the database
(make a new sheet on the file).
Table is arranged in the way you used to - rows and columns. Each row represents a record, e.g.
a customer. Each column contains the information of the record, e.g. the customer first name, the
customer Second Life (SL) UUID. The place where a row and column intersects, a particular cell
of a table, is called a field.
You can create as many tables as you like in a database, and arrange any number of rows and
columns you like in a table. They do not take large space in your web host as you can imagine just
to save some spreadsheets on the web host server.
You do not need to remember all of these names - only need to know the basic idea of how
database is arranged.
2 / 30
Creating Database
Login to your website control panel (cPanel), or somewhere that you can adjust your web account
settings. Figure 1 shows a sample screenshot of cPanel.
Figure 1
3 / 30
Figure 2
The basic setup of creating a database is completed. Figure 2 shows the current user in the
current database - jyprod_NewSL2.
4 / 30
phpMyAdmin
Similar to spreadsheet, you could use Word, Excel, Windows or Mac version etc., there are several
ways to manage database. In this tutorial, we will be using phpMyAdmin. It is commonly used and
allowing administer your MySQL databases in a user-friendly web based environment.
On the cPanel page find a link allow you to access to phpMyAdmin, similar on Figure 3.
Figure 3
5 / 30
Click on the link to enter the phpMyAdmin main page, as shown similar on Figure 4.
Figure 4
On left hand side, it shows the database name (jyprod_NewSL2 in the example) as well as any
tables (server and total) for the database.
By default, there is no table. Click on the database name and a page similar to Figure 5 will
appear. In this tutorial, we will create a database to store customer first name, last name and their
SL UUID.
Figure 5
6 / 30
Figure 6
3. It asks to enter the column information. Those are like the header information on a
spreadsheet, except there is more information such as the type of the colume (a text string,
integer number etc.). Input as shown on Figure 6 respectively:
Make sure highlight the option box in the key column to the key icon. The key column will be
unique from all rows, i.e. no repeat entry is allowed since all SL UUID is unique, although there
might have chances that customers to have the same first or last name.
Using VARCHAR, variable character, type is a text string format. The length is how many
characters (letters) we will assign for the column. The larger it is, the larger size is used on the
database hence your web host space usage.
7 / 30
The table named total2 is then created as shown on Figure 7 and there is no customer record
(row) by default. You could then add/remove customer within phpMyAdmin or use PHP scripts to
link between SL automatically.
Figure 7
8 / 30
Organizing Database By phpMyAdmin
You can do almost anything to the database within phpMyAdmin. In this tutorial, we will show you
how to insert customer, browse the current database, and export (backup) the current database.
Once you get used to the phpMyAdmin operations, the rest is simple and straight forward.
Figure 8
3. Untick the Ignore box if you would like to add 2 records on the same page. Press the Go
button on the bottom of the page to confirm. The 2 customer records are now added onto
the total2 table.
9 / 30
Browsing the database
Click the Browse tag on the top of the page. It lists the rows of the database. Figure 9 shows the
example of the 2 records just entered.
Figure 9
This concludes the phpMyAdmin section. You could also play around with different options such
as to add more records, add more columns, edit current records (click on the pencil icon), or
remove current records (click on the cross icon) etc.
10 / 30
Linking Database By PHP Script
Understanding PHP script basic
There is no direct link between LSL script to send customer data in SL and the out-world MySQL
database. In order to do that, we have to pre-written PHP script and store the script onto your web
host. Then using LSL to send customer data to the PHP script. When the PHP script listens the
customer data and sends the data to the database.
You could imagine PHP scripts are nothing but simply a text file ending with .php in the filename.
Although there are a few PHP editor with color for easier coding, they can be written on a simple
notepad or any text editor which does the same job.
We will run a few examples to show how to get the PHP scripts in action and communicate to the
database.
1. Login to SL, open the customer_register.php notecard comes with the purchased box.
2. Edit the first few lines upon your database settings earlier:
$user_name , $password, $database_name and $table_name.
4. Go back to the webpage where you have access of the web host server options, similar to
Figure 1 previously.
5. Select the File Manager icon, or anywhere similar that enables you to manage all files in your
web host server.
7. Copy and paste the saved notecard from step 2 to replace this plain text file.
Done. The PHP script is now sitting in your web host server and ready to receive customer
information from LSL scripts. Step 4 to 7 could be also done by FTP, file transfer protocol, to your
web host server if you know it already. Details on FTP is out of the scope for this tutorial.
The purchase box comes with 2 LSL scripts: Touch to register, and On_rez register. These 2
scripts have LSL functions that are able to send customer information to the PHP script
customer_register.php saved on your web host server. This is done by calling the PHP script file
address in your web host with the customer information as variables. When the PHP script
receives customer information, it then makes connection to the MySQL server and update the
database accordingly.
11 / 30
TOUCH TO REGISTER
This is a LSL script to be used on a prim that would usually to be a register board. When the board
is touched, it asks the person with a drop down menu if they are agreed to register. The script is
pre-written to work which you only need to change a few settings to suit your web host location.
1. Login to SL, rez the Touch to register object comes with the purchased box.
3. Edit the first line with your web host server address. This is the URL address from where you
saved the customer_register.php file. For example:
http://www.your_web_host.com/customer_register.php
When it is touched and the person chooses Agree from the drop down menu, his/her name as well
as the SL UUID will be sent to the PHP script. The PHP script will then send the information to
MySQL server and update the database automatically. The details on how each script lines work
will not be explained here, however, we will explain some LSL script lines in the last example of the
tutorial.
There will be IM sent to you (the owner of the board) after registration. There are 2 different IMs
depend on if the web host server is available or having down-time. For instance, Brad Pitt is just
registered, the IM will be either:
register [ Brad Pitt ] successful from PHP. (plus the board location SLURL)
or
register [ Brad Pitt ] backup from web host server not available. (plus the board location SLURL)
12 / 30
ON_REZ REGISTER
This is a LSL script to be used on a prim that would usually to be the box to your customer. When
your customer rezzes the box after purchase, it asks him/her with a drop down menu if they are
agreed to register. Again, the script is pre-written to work which you only need to change a few
settings to suit your web host location.
1. Login to SL, open the On_rez register script comes with the purchased box.
2. Edit the first line with your web host server address. This is the URL address from where you
saved the customer_register.php file. For example:
http://www.your_web_host.com/customer_register.php
3. Edit the 2nd line with your avatarʼs UUID. If you do not know your UUID, drop the Get Your
UUID object comes with the purchased box.
Done. When your customer rezzes the box, he/she will be asked to register.
Similar to the previous, when it is touched and the person chooses Agree from the drop down
menu, his/her name as well as the SL UUID will be sent to the PHP script. The PHP script will then
send the information and update the database automatically.
There will be IM sent to you (the owner of the database) after registration. There are 2 different IM
depends on if the web host server is available or having down-time. For instance, Brad Pitt is just
registered, the IM will be either:
register [ Brad Pitt ] successful from PHP. (plus the board location SLURL)
or
register [ Brad Pitt ] backup from web host server not available. (plus the board location SLURL)
13 / 30
Making Backup Of The Database
Backup of the database is essential just in case if the web host was no longer exists, or you would
like to change web host in the future etc. Also in order to send IM to all customers in SL on the
database, you must also backup the database to some LSL readable format. Again, this is
required as there is no direct link between MySQL to LSL.
There are a few backup formats phpMyAdmin provide. In this tutorial, we will be using backup to a
plain text file.
Figure 10
1. In phpMyAdmin, choose the Export tag from the top, as shown on Figure 10.
2. In the Export frame on the left hand side, select Open Document Text option.
3. Press the Go button on the bottom right.
4. Save the file. In this tutorial, the filename will be total2.odt - same as the tableʼs name.
Figure 11
14 / 30
Sending IM To Database Customers
After making backup on the text file. You are then able to use the data and send IM to all
customers in SL by using LSL.
For instance, we have a database with 10 customers registered. The backup text file will look like
Figure 12.
Figure 12
15 / 30
1. Highlight drag and copy from the first customer first_name (Tessa in this case) up to the last
customer_key (...873c5 in this case).
2. Login to SL, rez the Send IM to all customers object comes with the purchased box, open
the Backup Database notecard from its content.
3. Paste the customer data onto the notecard as shown on Figure 13. Remember the first line
on the notecard has to be started with // ===========...
Figure 13
16 / 30
Automatic Product Update
The automatic product update system consists of 2 modules as a pair: update server and its client.
The server will be a prim rezzed somewhere in SL with the latest version stored in its content. The
client will be the boxed object usually delivered to your customer.
Client sends the current version to the server as the box is rezzed. When the server listens the
client version, it checks if it is the latest version. If not, the server will delivery the latest version to
the customer.
1. When the purchased box is rezzed, it send an email* with its version number and customer
details to the server with its SL UUID.
2. When the server listens the data, it checks if the version number is the latest. If not, it sends
the latest version which is held in the serverʼs content via the received customer details.
Unfortunately any SL object, including the server, will have its UUID changes if it is re-rezzed.
Therefore the UUID stored in the client will have an unknown destination UUID for the email to
send. Thanks to the help of the out-world database, we are then able to store and update the
server UUID each time it changes. The client will not directly send email* to the server with a pre-
assigned UUID, instead it will get the UUID from the database and the server UUID will always be
the most updated.
Similar to the customer register table, you need to create a new table to store the server
information. The server information is fairly simple. We only need the server name (to be the
same as the product box name) and the server key (SL UUID).
Highlight the key icon of the server_key to make it to the unique entry. Done. The server table is
ready to use, as shown on Figure 14.
Figure 14
* There are a few SL prim-to-prim communication methods. Each client and the server are most likely to be in
different region (sim). To send prim-to-prim message regardless of any regions, it is required to use the llEmail() LSL
function. For more details, please visit http://wiki.secondlife.com/wiki/LlEmail
17 / 30
Establish the PHP script for the server
1. Login to SL, open the update_server_key.php notecard comes with the purchased box.
2. Edit the first few lines upon your database settings earlier:
$user_name , $password, $database_name and $table_name.
4. Copy and paste the saved notecard to a new plain text file with exact name as
update_server_key.php.
1. Login to SL, open the get_server_key_from_client.php notecard comes with the purchased
box.
2. Edit the first few lines upon your database settings earlier:
$user_name , $password, $database_name and $table_name.
4. Copy and paste the saved notecard to a new plain text file with exact name as
get_server_key_from_client.php.
18 / 30
Product update server
The server is a prim rezzed somewhere on SL in-world. It has to stay rezzed so that it can
listen the client for version update via email. One boxed product requires one server.
1. Login to SL, rez the Product Update Server object comes with the purchased box.
2. Edit the object name to be the exact name of the boxed product.
3. Edit the object description to be the version. Only 1 decimal place is allowed, e.g. 1.0
4. Drop the latest version boxed object into the server content. This will be sent to the
customer for update.
6. Edit the first line with your web host server address. This is the URL address from where you
saved the update_server_key.php file. For example:
http://www.your_web_host.com/update_server_key.php
Done. The server is now ready to use. Every time it is rezzed, its UUID will be sent and updated
to the server table in the database.
19 / 30
Product update client
The server is usually a boxed object delivered to your customer. When customer rezzes
the box, the box will first get the server UUID from the database. Then by using the
received server UUID, it will perform a llEmail() LSL function to send the current version to
the server. The server listens the version and checks if it is the latest. If not, the server
will send the update to the customer.
The client module is a LSL script. You may use your current box simply by adding the
script.
1. Login to SL, rez the product box - the box you use to pack items and delivery to customer.
2. Make sure the box name is identical as the server object name.
3. Edit the box description to be the version number. Only 1 decimal place is allowed, e.g. 1.0
4. Drop the On_rez check update script comes with this item purchased into the box content.
5. Open and edit the first line with your web host server address. This is the URL address from
where you saved the get_server_key_from_client.php file. For example:
http://www.your_web_host.com/get_server_key_from_client.php
Done. The client is now ready to use. Every time as the box is rezzed (e.g. from your customer
after purchase), the version will be sent to the server.
20 / 30
Sending Data from Website to SL Object (XML-RPC)
Previous sections were about having SL object to send information out-world. For instance,
TOUCH TO REGISTER allows SL resident touches the register board, his/her firstname, lastname
and UUID will then be sent and update the database accordingly.
AUTOMATIC PRODUCT UPDATE allows the box object to check its version when it is on-rez.
Sending the version number to the database and forward a later version accordingly.
However in certain situations where you would like just the opposite - getting information from the
database and even more: sending information from webpage to SL object. A formal term to be
used XML-RPC, that is a standard for sending procedure calls (e.g. function calls) to remote
systems. It sends XML data over HTTP that remote system then handles. Itʼs completely okay if it
doesnʼt make any sense. What we are interested here is what it can do and how to do it.
In the following example, I will show you how to make a webpage like shown on Figure 15. When
the color button is clicked, an SL object will have the color changed accordingly. It combines all
the use of database, sending and receiving information from and to PHP and LSL scripts - plus a
few html (xhtml) script lines.
Figure 15
21 / 30
In a nut-shell
There are billions and millions objects in SL. When we click on the color button, which object will
have its color changed? To begin, we have to firstly open (create) a channel for that particular
object that allows it to listen the message sent by clicking the color button. Similar to the product
update server, the channel is in SL UUID format (channel_key). It is not the same every time
hence it will be stored in database.
As the color button is clicked, we then get the channel_key from the database and send the color
data to the SL object through the channel. The object receives the message and changes its color.
Finally the SL object reports back to the website saying that the color change was successful.
All these can be accomplished by a few steps and will be explained below in more details:
2. Write a PHP script that gets the opened channel from the SL object, and stores it onto the
database.
3. Create a webpage to show the color buttons by writing a HTML script and a PHP script that
handles the color button response.
4. Write a LSL script that handles all channel creation, color info receivable as well as change
color etc.
22 / 30
Make a table to store channel_key
Create a new table called object_channel with 2 fields as shown on Figure 16.
channel_number, TINYINT
channel_key, VARCHAR, 36, key_column
If you forgot how to create a new table in database, please refer to page 6.
Figure 16
channel_number 1
channel_key default_key
as shown on Figure 17. If you forgot how to insert a record to a table, please refer to page 8.
Figure 17
23 / 30
Write a PHP script to handle the channel storage
Figure 17 shows the PHP script and it is included in the user manual in the purchase box.
Figure 17
Line 7 The channel_number field in the table. It is used to match in updating any
new channel created.
Line 9 The channel_key received from the SL object via a LSL script.
Line 23 Send (echo) a message after update channel_key successful, this message
will be received by the SL object via a LSL script.
Save the script as update_object_channel.php somewhere at your web host location. If you
forgot how to save PHP script, please refer to page 11.
24 / 30
HTML script to display the color buttons as website page
Figure 18 shows the HTML script and it is included in the user manual in the purchase box.
Figure 18
Line 1 to 4 A few essential lines to start on a HTML (to be more exact XHTML) script
nowadays. They are not compulsory if for purely HTML. However almost
99% of the webpages are now written as XHTML as it allows to use an
easier webpage style formatting (called CSS - cascading style sheets), it is a
good practice to include those lines. You donʼt have to know all of them as
they are out of the scope of the tutorial.
Line 9 to 10 A header line on the page, and the choose color message.
Line 12 to 22 A fieldset (rectangular frame) will wrap around all buttons. The buttons
are named color_button with individual color values. The button type is
submit that will “submit” the value to the action destination. The action
destination we are using is color_chosen.php. That means when either of
the button is clicked, its value will be sent and passed onto the
color_chosen.php script.
Save the script as color_change_page.html in the same folder at your web host location of the
update_object_channel.php script where you just saved previously.
25 / 30
PHP script to handle the color button clicked and contact SL object
Figure 19-A & B show the PHP script and it is included in the user manual in the purchase box.
Figure 19-A
26 / 30
Figure 19-B
Line 9 A check to make sure only does things when the color button is clicked.
Usually this color_chosen.php script page will only be called and loaded
after the button is clicked (the action destination in the html script). However
there is possibility that someone could manually type the file in the web
browser address bar, or bookmarked the location previously.
Line 18 to 21 Get the channel_key current stored on the database. The channel_key,
again, is selected upon the channel_number stated on Line 7.
Line 23 Write a notice message on the webpage to inform sending color information.
Line 25 to 57 Perform the action to send data to SL object via channel_key. Our sending
data will be the color (Line 26), the color buttonʼs value passed from the
color_change_page.html page. Line 34 shows the channel_key resulting
from the selection from the database. On Line 40 you could see the place
where the sending data goes.
Line 59 to 68 Perform a check to see if the message is sent and received by the SL object
via the LSL script successfully. Usually it takes less than 1 second for the
response, here we used 20 seconds to check just in case for slow
connection.
Save the script as color_chosen.php in the same folder at your web host location of the previous
scripts saved.
27 / 30
LSL script to handle the channel creation, and color change listened etc.
Figure 20-A & B show the LSL script and it is included in the user manual in the purchase box.
Figure 20-A
28 / 30
Figure 20-B
29 / 30
Line 1 Replace with the full address where you stored the
update_object_channel.php file in your web host.
Line 7 to 11 Whenever the script is first dropped into a SL object, it will open (create) a
channel. Any channel that is not used for 14 days will be cleaned up
automatically.
Line 13 to 29 Create the channel again by simply resetting the script in either of the
following 3 situations:
- If the object changes its region (sim) or teleported (e.g. worn on avatar).
- To open a channel when the object is rezzed, just in case if it's taken into
inventory for more than 14 days.
- To open the channel again less than 14 days (some time before it is
been cleaned up) if the object is rezzed all along.
Line 31 to 81 The remote data event will be triggered upon the channel is created as well
as when there is anything received via the channel.
Line 82 to 96 The http (webpage) response event will be triggered upon the channel was
update successfully in the database from the update_object_channel.php
script. The channel sender was on Line 37 to . Line 89 runs a timer event to
trigger Line 25 to 28 which opens the channel again some time less than 14
days (3600 seconds x 24 hours x 13 days = 1123200 seconds) if the object
is rezzed all along that time.
Drag and drop the script into a SL object that will run the color change demo.
update_object_channel.php
color_change_page.html
color_chosen.php
are saved in the same folder in your web host server with the correct database details on their first
few lines, as well as the out-world color change LSL script is in the SL object content with the
correct web host address on Line 1. Then all are good to go and you now know how to use
webpage buttons to communicate SL object in-world!
30 / 30