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:
13 | local s = game:GetService( [==[HttpService]==] ); |
17 | local json = s:JSONEncode(info); |
18 | local data = s:PostAsync( [=[--]=] , json); |
20 | local new = s:JSONDecode(d); |
PHP CODE:
03 | header( 'Content-Type: application/json' ); |
06 | $Array = json_decode($JSONReceived, true ); |
08 | $userID = intval($Array [ 'userID' ] ); |
16 | $conn = new mysqli($servername, $username, $password, $dbname); |
18 | if ($conn->connect_error) { |
19 | die( "Connection failed: " . $conn->connect_error); |
22 | $sql = "SELECT * FROM point_logs WHERE userid=$userID" ; |
23 | $result = $conn->query($sql); |
25 | $host_array = array(); |
26 | $timestamp_array = array(); |
27 | $pointsawarded_array = array(); |
28 | $description_array = array(); |
30 | $master_array = array(); |
32 | if ($result->num_rows > 0 ) { |
33 | // output data of each row |
34 | while ($row = $result->fetch_assoc()) { |
35 | array_push($host_array, $row [ "host" ] ); |
36 | array_push($timestamp_array, $row [ "time" ] ); |
37 | array_push($pointsawarded_array, $row [ "points_awarded" ] ); |
38 | array_push($description_array, $row [ "description" ] ); |
40 | $j 1 = json_encode($host_array, true ); |
41 | $j 2 = json_encode($timestamp_array, true ); |
42 | $j 3 = json_encode($pointsawarded_array, true ); |
43 | $j 4 = json_encode($description_array, true ); |
45 | array_push($master_array, $j 1 , $j 2 , $j 3 , $j 4 ); |
46 | $new_array = json_encode($master_array); |
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:
13 | local s = game:GetService( [==[HttpService]==] ); |
17 | local json = s:JSONEncode(info); |
and the same PHP code as describe above, it prints
1 | [ "[\"blankdata\"]" , "[\"2016-02-04 04:08:37\"]" , "[\"1\"]" , "[\"blankdatal\"]" ] |
2 | [ "[\"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).