Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Storing data outside sl

TheLoneWolf Arkin
Registered User
Join date: 26 Jan 2007
Posts: 29
12-15-2007 12:05
Hi,

i want to expand my scripting skills with storing data outside sl. now with minor expertise on php i want to ask for help by posting a little example i could start building of. i learn pretty fast as long as i have a working example :p lol

this is the basic i want to know how to do.

send a parameter to an external php file

this would be one of the following commands

add
remove
count (search?)

ok lets say i ask for adding something what line do i need to send an llHTTPRequest that send data with it like "action=add" and "object=item1" to the php page called "example.php" creatin an output saying that its passed or failed in sl.

same for remove, and a number for the count action.

then what is the php code going to look like? thats my main problem.

so how do i add something to a table (lets say by exmaple the table name is myobjects and the fieldname is objectname) in mysql using php, remove a line in a table in mysql, and count how many time a line repeats itself in a table in mysql? and outputting the info back to sl?

i dont know if the 3 routines are big as most of the examples i find on the net dont really work for me and yes my host has the latest php, mysql installed :D i think i miss something in the coding but as i said i just want to start with php so i hope IF these 3 routines are not to big i could see trough the basics of PHP/MySQL programming in combination with LSL.

Thank you in advance.
Tlw
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
12-15-2007 13:01
I actually posted this in an earlier thread, but I thought, I copy/paste it anway:

Explaining the whole PHP-Process would be a little beyond the scope here... Google for tutorials that explain storing stuff in a MySQL-DB, that might be helpful.

The Process is something like:

-> send a llHTTPRequest from SL to PHP (using POST!)

(Pseudo-Code! - just posting untested snippets here...)

CODE


key my_request;

my_request = llHTTPRequest("http://my.server.com/my-script.php",
[HTTP_METHOD, "POST",
HTTP_MIMETYPE, "application/x-www-form-urlencoded"],
"sl_var1=hello&sl_var2=world");

http_response(key request_id, integer status, list metadata, string body)
{
if (my_request == requestid)
{

// The body-var is the return-value of your php-script, see below

llOwnerSay(body);
}
}



-> fetch the variables with PHP

CODE
 
$sql = "INSERT INTO my_table sl_var1, sl_var2 VALUES('".$_POST['sl_var2']."', '".$_POST['sl_var2']."')";

mysql_query($sql);

echo "the entry has been stored in the database"; // this will be «heard» by the calling SL-Script, resulting in the «body» variable in SL



Everything you print out in PHP using «echo» will be sent back to the calling SL-Script. So if you just wanna know, whether something was found or not, use «echo "ok"» or «echo "failed"» - you'll receive either «ok» or «failed» in the body-part in the body-var of the http_request-event.

There's a good example on the Wiki:

http://www.lslwiki.net/lslwiki/wakka.php?wakka=ExamplellHTTPRequest

As for the MySQL-Connection, have a look at www.php.net - they've got pretty good examples for that...

For the MySQL-Syntax - here are some (pseudo-code) examples:

CODE

// removing something from a table

$sql = "DELETE FROM tbl_mytable WHERE id_mytable='".$_POST['id']."'"

$result = mysql_query($sql);

// or to delete a specific name:

$sql = "DELETE FROM tbl_mytable WHERE objectname='".$_POST['objectname']."'"
$result = mysql_query($sql);

// Be aware, that the entry in the field 'objectname' has to be unique (or whatever field you choose for comparison. Otherwise every row where the field objectname contains the comparison gets deleted.

// To count, how many times 'objectname' is in the table, use

$sql = "SELECT COUNT(objectname) FROM tbl_mytable WHERE objectname = '".$_POST['objectname']."'";

$result = mysql_query($sql);

//In Row 0 of your mysql-reply you'll get a field called 'COUNT(objectname)' - this will contain the number of rows found;



For more informations on MySQL-Commands have a look a the MySQL-Manual (found at www.mysql.com)...

And have a look here, for howto fetching the results of a mysql-query:

http://ch2.php.net/manual/de/function.mysql-fetch-row.php

HTH
TheLoneWolf Arkin
Registered User
Join date: 26 Jan 2007
Posts: 29
12-15-2007 13:04
tnx i'll give it a try.