Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Help, MySQL + llHTTPRequest (wont load into my database...) PLZ HELP! =D

Kyle Kamachi
Registered User
Join date: 4 Feb 2007
Posts: 16
06-20-2007 20:43
OK So basically this is what i have, when someone clicks on this in the game it is supose to update my database with there player name, then spit it back out into game, the only problem that i have so far is that it wont upload into my database...im sure its not a problem with the SL code but something with the PHP, i have provided both sets of code here


!---------SL CODE--------!

key req_id;
string name;
integer lplaycount;
default
{

state_entry()
{
integer lplaycount = 0;
}

touch_start(integer total_number) {
name = llDetectedName(0);
req_id = llHTTPRequest("http://kklouzal.awardspace.com/secondcom.php", [HTTP_METHOD, "POST"], "name=" + name);
++lplaycount;
}

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

llSay(0, body);
llSetText("Current Local Players: " + (string)lplaycount + "\nLatest Local Player: " + name + "\n-------------------------\n Current Global Players: " + "\nLast Global Player: ", <0.0,1.0,0.0>, 1);
}
}
}






!---------------PHP CODE----------------!





<?
$username="kklouzal_account";
$password="*********";
$database="db4.awardspace.com";
$db="kklouzal_account";
$p_data = implode('', file('php://input'));
$p_data = explode('&', $p_data);

foreach ($p_data as $p_val) {
$d_parts = explode('=', $p_val);
$_POST[$d_parts[0]] = urldecode($d_parts[1]);
}


$name = $_POST['name'];

$return = $name." have touched me";

mysql_connect($database,$username,$password);
@mysql_select_db($db) or die( "Unable to select database";);

$query = "INSERT INTO accounts VALUES ('', $name)";

mysql_query($query);
mysql_close();
echo $return;
?>
Pavcules Superior
Registered User
Join date: 30 Oct 2006
Posts: 69
06-21-2007 05:42
There is a several things you can try to see if the PHP script and database is working.

- Check that the DB User account "kklouzal_account" has got permissions to write to the database.

- Try running the PHP script via your web browwer.
Firstly, change the "_POST" part to a "_GET", e.g.: $name = $_GET['name'];

Then, open your webbrowser and enter the following: http://kklouzal.awardspace.com/secondcom.php?name=Test
Press Enter to go to that URL.

If you see the results of your page, and see that the database has being updated, then you know that the PHP works correctly. Replace the "_GET" back to "_POST".

- Is the database name in the $database="db4.awardspace.com"; variable correct? Usually its the database name without webname part. Depends on how on this is setup by your provider, ettc.

- If that doesn't help, try adding it more debug information by reporting the variable information.
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
06-21-2007 06:39
The following works for me with PHP v4.4.6

<?php
function emu_getallheaders()
{
foreach($_SERVER as $name => $value)
if(substr($name, 0, 5) == 'HTTP_')
$headers[str_replace('X-Secondlife-', 'X-SecondLife-', str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))))] = $value;
return $headers;
}

// \/ \/ \/ \/ from http://rpgstats.com/wiki/index.php?title=ExamplellHTTPRequest \/ \/ \/ \/
//Get POST variables and put into superglobal array
$p_data = implode('', file('php://input'));
$p_data = explode('&', $p_data);

foreach ($p_data as $p_val) {
$d_parts = explode('=', $p_val);
$_POST[$d_parts[0]] = urldecode($d_parts[1]);
}
// /\ /\ /\ /\ **************************************************************** /\ /\ /\ /\

$headers = emu_getallheaders(); // Just replace any use of `apache_request_headers()` with `emu_getallheaders()`.

//$objectName = $headers["X-SecondLife-Object-Name"];
$objectKey = $headers["X-SecondLife-Object-Key"];
//$ownerName = $headers["X-SecondLife-Owner-Name"];
$ownerKey = $headers["X-SecondLife-Owner-Key"];
$region = $headers["X-SecondLife-Region"];

$guestName = $_POST["name"];
$dbh=mysql_connect ("localhost", "username", "password";) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("databasename";);

mysql_query("INSERT INTO guestbook (guestName) VALUES('$guestName') ";) or die(mysql_error());
$status = "Thank-you, $guestName, Your comments have been added.";
echo $status;
?>
_____________________
www.nandnerd.info
http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
06-21-2007 08:36
I'm certainly no expert with PHP or MySQL but the following line looks a little suspect:

$query = "INSERT INTO accounts VALUES ('', $name)";

If you are getting the response from echo $return; (which is output in SL by llSay(0,body);) okay then I suspect it's an issue with your database query.

$query = "INSERT INTO accounts name='$name'";

Might be correct but I don't have my reference book.

Adding the "or die(mysql_error());" to your "mysql_query($query);" would give you an error if your query is the problem so that is worth a shot too:

mysql_query($query) or die(mysql_error());
_____________________
www.nandnerd.info
http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
whyroc Slade
Sculpted and Blended
Join date: 23 Feb 2007
Posts: 315
06-21-2007 11:31
yes using the VALUES command implies you have more than one value in a comma delimited sequence. You are not giving the query your column heading name.


"INSERT INTO birthdays (name, birthday) VALUES ('Peggy', 'June4')"

-why
Kyle Kamachi
Registered User
Join date: 4 Feb 2007
Posts: 16
06-21-2007 14:28
Ok so i changed o the GET method, still did bot work then i added 2 variables that the script sent and it still does not work...