Using llHTTPRequest to store and Retreive Data in SL
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-01-2007 14:02
Hello,
Im fairly decent at LSL (Not an expert by any means) and fairly decent at editing PHP etc.
This might have been asked a few times but.
What would I need to write in LSL Script to send data to a database named test with a table named testamount. so the table would be like: Table: Testamount Columns: username numberofapples numberoforanges
So the prim would send the data to mysql server with the username and the number of apples and number of oranges. If the user doesnt exist create the new username (The Owner of the Object) in the database.
Then in reverse I want it to Get the data from the mysql server and llSay in SL the username and how many apples and oranges they have.
Can anyone give me a basic LSL and PHP script that would do this that I can build off of? Or in the very least point me in the direction of a guide for it?
Thank you so much for your time! -Cherry Hotaling
|
|
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
|
11-01-2007 14:25
One of my really-I-must-do-this-when-I-have-the-time projects is to build a simple example of how this sort of thing works, because I don't know of any, but it is hard to explain in one post. The bare bones would be something like the following: (a) An LSL client script which uses llHTTPRequest to send requests to your server, with parameters indicating the sort of command that should be carried out (e.g. "add avatar X to the user list"  specified as GET or POST, and which has http_response() event code to interpret whatever it is the server sends bac; (b) A server with database access, which probably means MySQL; (c) A PHP or other script on that server which can interpret HTTP requests from the LSL script in (a), carry out the appropriate actions and then report the results. This will mean reading up on the mysql_ functions in PHP if that is what you are writing it in.
_____________________
http://ordinalmalaprop.com/forum/ - visit Ordinal's Scripting Colloquium for scripting discussion with actual working BBCode!
http://ordinalmalaprop.com/engine/ - An Engine Fit For My Proceeding, my Aethernet Journal
http://www.flickr.com/groups/slgriefbuild/ - Second Life Griefbuild Digest, pictures of horrible ad griefing and land spam, and the naming of names
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-01-2007 16:00
Right and I have full control access to my linux server and it has php and mysql and I admin mysql with phpmyadmin etc. Just need to see how the script in LSL and php looks and I could build off of that you know?
-Cherry Hotaling
|
|
Checho Masukami
UnRez it or use a hammer
Join date: 6 Oct 2006
Posts: 191
|
11-01-2007 18:43
My latest projects uses a lot of this, sending data to a external server, saving it in databases and resending info to SL. The process is somethig like this: Send the info to a php file in your server using variables in the http address. Example: http://myserver.com/write.php?variable1=5000&variable2=Checho The file write.php reads the variables and stores them in a MySQL database. If you need to retrieve the info, request something like this from LSL: http://myserver.com/read.php?variable1=5000 The file read.php reads, for example, the MySQL stored value for variable1 = 5000 and using "echo" or "print" gives the value "Checho" to LSL. You can also use XML if you need to send info to LSL without need of request it but that is a little more complicated.
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-01-2007 19:39
would you have an example LSL and php file for this hun?
I would appreciate it greatly
-Cherry Hotaling
|
|
Twisted Pharaoh
if ("hello") {"hey hey";}
Join date: 24 Mar 2007
Posts: 315
|
11-01-2007 23:09
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-02-2007 07:37
thats awesome Thank you Twisted but:
Does anyone have an example of what I was saying with MySQL by any chance?
The MYSQL part is where im stuck the most on.
In the meantime I will play around with those examples.
-Cherry hotaling
|
|
Twisted Pharaoh
if ("hello") {"hey hey";}
Join date: 24 Mar 2007
Posts: 315
|
11-02-2007 08:54
Hmm, did you click on Display Comments/Form at the bottom of the page? I know it's not easy to spot hehe.
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-02-2007 10:08
The Store function thats on there I think might be too complex for me to get a grasp of the basics of it.
Any other basic examples possibly?
Thanks -Cherry Hotaling
|
|
Twisted Pharaoh
if ("hello") {"hey hey";}
Join date: 24 Mar 2007
Posts: 315
|
11-02-2007 10:52
What you need is a PHP+MySQL tutorial, since you got the LSL part. You can read this one, old but still valid, or find another on the web. http://www.webmonkey.com/webmonkey/programming/php/tutorials/tutorial4.htmlit really starts here: http://www.webmonkey.com/webmonkey/99/21/index2a_page4
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-02-2007 11:48
Ok thank you, I was following this before you posted: http://www.freewebmasterhelp.com/tutorials/phpmysql/1It seems to be decent I will check out the links you gave me also. I think my question eventually will be how it sends the form into to the php and how I can select certain data what it comes back into SL etc. -Cherry Hotaling
|
|
Twisted Pharaoh
if ("hello") {"hey hey";}
Join date: 24 Mar 2007
Posts: 315
|
11-02-2007 13:29
You'll get the SL parameters into php like this (if you follow the example from Checho) : $variable1 = urldecode($_REQUEST['variable1']) ; I think your other questions will be answered by these different links but feel free to ask 
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-02-2007 20:04
ok im starting to get it now what do i write to have the Scripted prim listen for the data back from the server. For Example I wrote this after following a couple of tutorials: ########################## ## Search.php Script on Web Server ## ########################## $dbusername="*******"; $dbpassword="*******"; $database="tutorial"; $usersearch=$_POST['username']; mysql_connect("localhost",$dbusername,$dbpassword); @mysql_select_db($database) or die( "Unable to select database"  ; # $query="SELECT * FROM fruit"; $query="SELECT * FROM fruit WHERE username='$usersearch'"; $result=mysql_query($query); $num=mysql_numrows($result); # $userselect="SELECT * FROM fruit WHERE username='tester2'"; mysql_close(); $i=0; while ($i < $num) { $username=mysql_result($result,$i,"username"  ; $apples=mysql_result($result,$i,"apples"  ; $oranges=mysql_result($result,$i,"oranges"  ; $bannanas=mysql_result($result,$i,"bannanas"  ; echo "<b>$username</b><br>Apples: $apples<br>Oranges: $oranges<br>Bannanas: $bannanas<br>"; $i++; } ?> ########################## ## Sl Script ## ########################## ?? No Idea Here ?? I Want to click a prim on a touch event send a username to my search.php form and return the data and llSay whats Echod in the search.php script Any help here? Thanks for your time -Cherry Hotaling
|
|
Twisted Pharaoh
if ("hello") {"hey hey";}
Join date: 24 Mar 2007
Posts: 315
|
11-02-2007 21:47
You need to adapt the source code presented here: http://www.lslwiki.net/lslwiki/wakka.php?wakka=ExamplellHTTPRequestWith your URL and parameters, so only username here.
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-05-2007 07:14
Ok im trying to follow that but the guy has a bunch of stuff for his store in the code and I cant even tell whats for The Prim and whats for php.
It looks like because the <? php tag at the top that thats for his web php file (Like the example I created above)
But trying to pick out the MySQL insert from the prim and how it gets the echo back from the web I dont understand.
I just need an example thats simple like:
Prim (lsl example needed) send number to php file on the web, php file inserts or updates the number in the mysql server and the Php file echos the number back to SL and the prim calls out the number stored so you know it was successful.
Can anyone give me a more simplified example like that?
Thank you so much for your time, -Cherry Hotaling
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-06-2007 06:05
Can anyone help with this?
Pretty please?
-Cherry Hotaling
|
|
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
|
11-06-2007 06:46
From: Cherry Hotaling I just need an example thats simple like: Prim (lsl example needed) send number to php file on the web,
http = llHTTPRequest("http://www.yourdomain.com/page.php", [HTTP_TYPE, "POST"], "?value1="+(string)value1+"&value2="+(string)value2); // where http is a global integer.
From: Cherry Hotaling php file inserts or updates the number in the mysql server
<?php $value1 = $_POST['value1']; $value2 = $_POST['value2']; $dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("databasename"); $result = mysql_query("SELECT * FROM tablename WHERE value1='$value1' ") or die(mysql_error()); $row = mysql_fetch_array($result); if ( $row['value1'] != "" ) { mysql_query("UPDATE tablename SET value2='$value2' WHERE value1='$value1' ") or die(mysql_error()); $database_status = "Updated database"; } else { mysql_query("INSERT INTO tablename (value1, value2) VALUES('$value1' , '$value2' ) ") or die(mysql_error()); $database_status = "Inserted data into database"; } // ... continues below
From: Cherry Hotaling and the Php file echos the number back to SL
// ... continued echo "value1 is $value1, value2 is $value2, $database_status"; ?>
From: Cherry Hotaling and the prim calls out the number stored so you know it was successful.
in the http_response event, llSay (or llOwnerSay, llWhisper etc) the data received. http_response(key id,integer status, list meta, string body) { if(http==id) { llSay(0,body); } }
P.S. untested P.P.S. note that the [ php ] and [ /php ] tags are only for forum formatting (which is disabled), to see the page formatted search the forums for "greasemonkey".
_____________________
www.nandnerd.info http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-06-2007 10:03
*Clutches onto Nand and hugs you to death*
Thank you so much hun!
I will try this when I get home today yay!
Much easier to understand.
So For value1 etc I would put like an integer like 4 or a number etc?
That would pass it off to the webserver.
I cant wait to try this at home.
Thank you -Cherry Hotaling
|
|
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
|
11-06-2007 10:40
From: Cherry Hotaling So For value1 etc I would put like an integer like 4 or a number etc?
value1 is your unique identifier for the database. If you were gathering data on a specific person that would be their name or key. If you were gathering data on a specific region that would be the region name or corner coordinates. If you were gathering data on something which changes over time it might be the unix time stamp. Whatever you use to uniquely define one row from another. The values are not limited to integers, if you're using a string which is passed to the script by chat then you'll need to use something like llEscapeURL (LSL) or htmlentities() (PHP) to turn it into a safe string so malicious data doesn't make it into your database and eventually onto a webpage or your website. A few good resources for PHP are: http://www.php.nethttp://www.w3schools.com/php/default.asphttp://hudzilla.org/phpwiki/index.php?title=Main_Pageand of course google, just type in a function name and "php" will usually point you in the direction of an example or explanation of the function.
_____________________
www.nandnerd.info http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-06-2007 11:55
Ok so:
If I wanted to send the Number 2 to the php script I would do something like:
integer value1 = 2;
at the beggining of the Prim script?
Like:
integer value1 =2;
http = llHTTPRequest("http://www.yourdomain.com/page.php", [HTTP_TYPE, "POST"], "?value1="+(string)value1+"&value2="+(string)value2);
This would send the number 2 as value1 to the php script?
Thanks for your time btw -Cherry Hotaling
|
|
FlipperPA Peregrine
Magically Delicious!
Join date: 14 Nov 2003
Posts: 3,703
|
11-06-2007 12:30
Gawd, I love how wonderfully readable the code on this form is! Gogomodo SLTrivia uses this constantly. llHTTPRequest is my favorite function ever... I started begging for it in 2003/2004. Now all we need is incoming HTTP request instead of polling! Place your votes here! http://jira.secondlife.com/browse/SVC-913The only thing I'd do differently is assigning a channel instead of using object UUID. Regards, -Flip
_____________________
Peregrine Salon: www.PeregrineSalon.com - my consulting company Second Blogger: www.SecondBlogger.com - free, fully integrated Second Life blogging for all avatars!
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-06-2007 12:44
Hi FlipperPA Peregrine,
I appreciate your AD for your cause. However what does your reply have to do with my question in any way?
-Cherry Hotaling
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-06-2007 17:02
Aha I got it!!!!
Thank you so much Nand that Example was perfect!!
-Cherry Hotaling
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-06-2007 19:25
Ok I have one other question. Can I use integers in the part that sends the post data to the php file? For example int he example below I want to change: "username=ownerkey&apples=18"  ; Where it says ownerkey I want that to be the contents of the variable ownerkey which is the Username of the owner of the object. How would i re write that one line to use a variable? Thanks -Cherry Hotaling string ownerkey; // Ownerkey Global Variable key requestid; default { state_entry() { ownerkey = llKey2Name(llGetOwner()); //Get the Owner of the Objects Username } touch_start(integer number) { requestid = llHTTPRequest("http://BLAHBLAHBLAH.com/test.php", [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], "username=ownerkey&apples=18"  ; } http_response(key request_id, integer status, list metadata, string body) { if (request_id == requestid) llWhisper(0, "Web server said: " + body); } }
|
|
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
|
11-06-2007 19:39
Thank you to Bloodsong for answering this one for me. The answer was to build a string such as: "username="+ownerkey+"&archery=18"  ; Thanks Blood.
|