Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Want to read Data from external DB with a lsl-script

Tomfox Wiranata
Registered User
Join date: 20 Dec 2006
Posts: 80
03-08-2007 09:23
Hi guys!

I have a table in an external DB on a webserver. I need to read out these data with a sl-script. I came so far:

This is my php-script on the webserver
CODE

<?php

// Database Settings
define('DB_HOST','****'); // MySQL Server Host
define('DB_USER','****'); // MySQL User
define('DB_PASS','***'); // MySQL Password
define('DB_NAME','***'); //The name of the database

//Connect to the database or throw a nasty error
$db = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME,$db) or die(mysql_error());



switch(strtoupper($_REQUEST['command'])){
case 'GET':
cmd_get($_REQUEST);
break;

default:
echo ("Invalid Command Requested");
}

//close the database connection
mysql_close($db);

//stop execution
die();




function cmd_get($params){
// Check for required parameters
$req = array('slname','slid');
foreach($req as $rkey){
$$rkey = mysql_real_escape_string($params[$rkey]);
}


$sql = "SELECT `name`,`id` FROM `tableBlaBla`";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($result)){
echo $row['name']." ";
echo $row['id']."\n";
//echo "tut";
}
}

?>



If i type in the link http://www.myPage.com/MySqlExample.php?command=get both information, as name and id, get listed. But somehow I do not get it in SL.


here is my sl-script

CODE


key saveData;


default
{
touch_start(integer toucher)
{
urlStore = "http://myPage.com/Script.php?command=get&slname=name&slid=id";
saveData = llHTTPRequest(urlStore,[HTTP_METHOD,"GET"],"");
llInstantMessage(llDetectedKey(0),saveData);
}
}


thx for your time and effort!
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
03-08-2007 09:35
Your result will not be in SaveData. This is just the key / id of the request.
You need to handle the http_response event to get the actual data from the server.
Tomfox Wiranata
Registered User
Join date: 20 Dec 2006
Posts: 80
03-08-2007 09:56
hmm....ok...and how do i do that? do i need to change the php-script?
Alidar Moxie
Registered User
Join date: 2 Dec 2005
Posts: 47
03-08-2007 10:51
CODE

key saveData;


default
{
touch_start(integer toucher)
{
urlStore = "http://myPage.com/Script.php?command=get&slname=name&slid=id";
saveData = llHTTPRequest(urlStore,[HTTP_METHOD,"GET"],"");
}

http_response(key id,integer status, list meta, string body)
{
if(saveData==id)
{
llOwnerSay(body);
}
}

}



Of course you might want to change 'saveData' to something like 'saveKey', but you get the point.

Get more info from http://www.lslwiki.net/lslwiki/wakka.php?wakka=http_response
Tomfox Wiranata
Registered User
Join date: 20 Dec 2006
Posts: 80
03-08-2007 16:06
thx...it worked....

the body variable is type string. the information is the key of each resident. i need to implement the body in the llGiveMoney function. But as a string it is not possible, cause llGiveMoney demands type key :(

can i parse a string to a key? did not find anything. but maybe i am wrong.

thx again :)
Squirrel Wood
Nuteater. Beware!
Join date: 14 Jun 2006
Posts: 471
03-09-2007 00:53
What you need to do is called "typecasting"

You can do this in SL by simply putting the desired variable type before the variable that is of a different type.

CODE

string blah = "0000-00-00-0000";
key X;

X = (key)blah;


In your case:
llGiveMoney((key)body,amountofmoney);
Tomfox Wiranata
Registered User
Join date: 20 Dec 2006
Posts: 80
03-09-2007 01:59
well that makes sense ^^

thanks you guys... wish u all a nice weekend