Hello,
I have recently began development of a database for a group. Within my database, I have a section for points (you earn them @ trainings etc), discipline, medals, and logs. My end goal is something similar to that of RAT's InfoCenter.
Now, I have the points and discipline parts working. The logs and medals, however, are a problem because I am trying to fetch an array from the SQL database. The PHP code I have developed can do this, but the true problem lies in getting the array from the server back to roblox. The array must also be able to be iterated through.
Every time I use "json_encode($array);" (<< that code is PHP) and I echo it, it leads to an error when I try to decode it on the roblox server. The error is "Can't parse JSON". I tried both playing it in studio, and in game both of which fail to get the results I need.
Without further explanation, I will provide my code.
LUA CODE:
-- receives userid via remote function
-- passes userid to GET_STRIKES_API in web server
--function_ = game.ServerStorage.operating_system_q.remote_functions.invoke_f_strikes
-- ignore above ^
function get()
local id = 17134610
print("starting")
local s = game:GetService([==[HttpService]==]); -- / Assumes HttpEnabled is true
local info = {
userID = id;
}
local json = s:JSONEncode(info);
local data = s:PostAsync([=[--]=], json);
local d = data
local new = s:JSONDecode(d);
print(new)
end
get()
PHP CODE:
<?php
header('Content-Type: application/json');
$JSONReceived = file_get_contents("php://input");
$Array = json_decode($JSONReceived, true);
$userID = intval($Array['userID']);
$servername = "--";
$username = "--";
$password = "--";
$dbname = "--";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM point_logs WHERE userid=$userID";
$result = $conn->query($sql);
$host_array = array();
$timestamp_array = array();
$pointsawarded_array = array();
$description_array = array();
$master_array = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
array_push($host_array, $row["host"]);
array_push($timestamp_array, $row["time"]);
array_push($pointsawarded_array, $row["points_awarded"]);
array_push($description_array, $row["description"]);
$j1 = json_encode($host_array, true);
$j2 = json_encode($timestamp_array, true);
$j3 = json_encode($pointsawarded_array, true);
$j4 = json_encode($description_array, true);
array_push($master_array, $j1, $j2, $j3, $j4);
$new_array = json_encode($master_array);
echo $new_array,"\n";
}
} else {
echo "error";
}
?>
The database contains two log entries with the said userid found in the lua code. Thus, the while($row = $result->fetch_assoc()) section runs twice. Obviously, I have the correct server name, username, database name, and password. I simply deleted those so you don't decide to log into my database ;)
If you have any questions, please ask.
And thanks in advance.
UPDATE With the following lua code:
-- receives userid via remote function
-- passes userid to GET_STRIKES_API in web server
--function_ = game.ServerStorage.operating_system_q.remote_functions.invoke_f_strikes
-- ignore above ^
function get()
local URLs = {}
local id = 17134610
print("starting")
local s = game:GetService([==[HttpService]==]); -- / Assumes HttpEnabled is true
local info = {
userID = id;
}
local json = s:JSONEncode(info);
local data = s:PostAsync([=[http://forfoxarmy.esy.es/database/Logs/GET_PLOGS_API.php]=], json);
--local a = s:GetAsync("--", true)
local d = data
print(d)
--local wfwe = s:JSONDecode(a)
--print(wfwe)
end
get()
and the same PHP code as describe above, it prints
["[\"blankdata\"]","[\"2016-02-04 04:08:37\"]","[\"1\"]","[\"blankdatal\"]"]
["[\"blankdata\"]","[\"2016-02-04 04:08:37\"]","[\"1\"]","[\"blankdatal\"]","[\"blankdata\",\"blankdata\"]","[\"2016-02-04 04:08:37\",\"2016-02-04 04:08:40\"]","[\"1\",\"1\"]","[\"blankdata\",\"blankdata\"]"]
The information "blankdata", "2016-02-04 04:08:37", and "1" were information inputted in my SQL database (not random things being printed).

6 comments
I know little of PHP, but what happens when you print the encoded JSON on the ROBLOX side? HTTPService:JSONDecode is expecting a string to decode.
Have you tried printing the data to make sure you're getting back what you think you are? e.g., is it possible you're getting back "error" ?
Can you post what the PHP file is outputting?
Yea, sure.
["["blankdata"]","["2016-02-04 04:08:37"]","["1"]","["blankdata"]"] ["["blankdata"]","["2016-02-04 04:08:37"]","["1"]","["he did well"]","["xEnforcer","xEnforcer"]","["2016-02-04 04:08:37","2016-02-04 04:08:40"]","["1","1"]","["blankdata","blankdata"]"]
^ that is information from my SQL database being printed