- You have a web server and a place that this database/web script can go.
- You have a database and don't need help setting one up
- You have at least very basic knowledge of PHP and writing MySQL requests.
That being said, let's get started. The first thing you will need is to set up your database and know what you are going to store. For this example let's say you want to insert the name of an avatar into a database whenever the object is touched, and keep a running tab of how many times they have touched it.
Assume you have a database called "SL" with a table called "stats". Stats has two columns, "name" and "number". Name will be the name of the avatar (for this example it would need to be set as unique). Number will represent the number of times the prim was clicked.
Now what we are going to do is set up our PHP script. What will happen is that every time the prim is clicked, it will send a request to this PHP script, which will then update the database. Here is an example of what the PHP script should look like for this example.
<?php
$db = mysql_connect("localhost", "db_username", "your_password") or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.')</script></html>");
mysql_select_db("SL",$db);
$name = $_GET['name']; //$_GET takes variables that are passed from the url (see the LSL example)
$usertable = "stats";
$update = "INSERT INTO $usertable SET name = '$name', number = 1 ON DUPLICATE KEY UPDATE number = number + 1";
$result = mysql_query($update);
?>
And now for the LSL script that will send the request to the PHP page:
string owner_name;key owner_name_query;default
{state_entry()
{
}
touch_start(integer total_number)
{
owner_name_query = llRequestAgentData(llGetOwner(), DATA_NAME);
}
dataserver(key queryid, string data){
if (owner_name_query == queryid){owner_name = data;llHTTPRequest("http://www.yoursite.com/yourphppage.php?name=" + owner_name, [], "");
}
}}
Let's take a look at the URL in the llHTTPRequest function. http://www.yoursite.com/yourphppage.php?name=" + owner_name. Remember the $_GET['name'] part of the PHP script? Well this is where that variable is taking data from. ?name= denotes a variable that can be passed to PHP using a URL. If you want to pass more variables, you can string them along with ampersands like this: ?name=Joe_Shmo&id=123456&status=living
Well I hope that is enough to get you started loading things into databases. Next time we can talk about how to get data back out of the database.
Well I hope that is enough to get you started loading things into databases. Next time we can talk about how to get data back out of the database.
0 comments:
Post a Comment