Pages

Showing posts with label llRequestAgentData. Show all posts
Showing posts with label llRequestAgentData. Show all posts

Wednesday, May 26, 2010

Dave's Giant Database Insertion Tutorial

So, I've had a request to explain inserting data into an external database from SL. This is a pretty lengthy subject but to shorten it up for this post I'm going to assume a few things, such as:
  1. You have a web server and a place that this database/web script can go.
  2. You have a database and don't need help setting one up
  3. You have at least very basic knowledge of PHP and writing MySQL requests.
As a side note, everything on this page that is highlighted in orange is something that you will need to rename yourself.

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.

Tuesday, May 18, 2010

Get a bunch of info on someone (llRequestAgentData example)

Shila asked how to get an avatar's name , DOB, Payment Info  and online status. Doing this can be lengthy because each of these things need to be handled differently as they return different data types. Here is an example script I wrote this afternoon that gets all of these pieces of information:

key online_query;
string name_query;
string birthday_query;
string payinfo_query;
string owner_name;
string online;
string birthday;
string payinfo;

default
{
   touch_start(integer total_number)
    {
        key id = llDetectedKey(0);
        name_query = llRequestAgentData(id, DATA_NAME);
        online_query = llRequestAgentData(id, DATA_ONLINE);
        birthday_query = llRequestAgentData(id, DATA_BORN);
        payinfo_query = llRequestAgentData(id, DATA_PAYINFO);
    }
    dataserver(key queryid, string data)
    {
        //Find out their name --------------------------------------
        if ( name_query == queryid )
        {
            owner_name = data;
            llSay(0, "The name of the person is : " + owner_name );
        }
       
        //Find out if they are online-------------------------------
        if ( online_query == queryid )
        {
              online = data;
            if (online == "1"){
                llSay(0, "he or she is online");               
            }
            else
            {
                llSay(0, "he or she is offline");          
            }
        }
       
        //Find when their birthday is -------------------------------
        if ( birthday_query == queryid )
        {
            birthday = data;
            llSay(0, "Their birthday is "+ birthday);
        }
       
        //Find out what their payment info status is ----------------
        if ( payinfo_query == queryid )
        {
            payinfo = data;
            if (payinfo == "0"){
            llSay(0, "They have no payment info on file");
            }
            if (payinfo == "1"){
            llSay(0, "They have payment info on file");
            }
            if (payinfo == "3"){
            llSay(0, "Their payment info has been used");
            }//end if(payinfo == "3")
        }//end query if
    }//end dataserver
}//end default
There are a few things you'll notice right away. First of all I have this set up so it gets the info of whoever touched the prim. The next thing is that I have four queries set up for each of the pieces of data we want to get, and finally inside the dataserver event we have four "if" statements that handle each of the queries. The name query returns the avatar's name as a string, their online status returns as an integer disguised as a string (either 1 or 0 depending on their online status), their birthday returns as a string in the format YYYY-MM-DD, and their payment info also returns as integers in string format, 0 for no payment info, 1 for pay info on file, and 3 for pay info used. Don't ask what happened to 2.

So now you know how to get most of the data on someone's avi. If you want to do other things with this info you can put your functions in the dataserver event, inside the if statement pertaining to that piece of information.