Hey, I want to get informations from my MySQL Database and print it in Roblox.
This is my current DataBase :
So this is my current Roblox Script that ask my PHP page for informations :
local function request()
local response = HttpService:RequestAsync(
{
Url = "MyWebPage", -- This website helps debug HTTP requests
Method = "POST",
Headers = {
["Content-Type"] = "application/json" -- When sending JSON, set this!
},
Body = HttpService:JSONEncode({Request = "PlayerInformations"})
}
)
-- Inspect the response table
if response.Success then
print("Status code:", response.StatusCode, response.StatusMessage)
print("Response body:\n", response.Body)
else
print("The request failed:", response.StatusCode, response.StatusMessage)
end
end
-- wrap the function in a 'pcall' to prevent the script from breaking if the request fails
local success, message = pcall(request)
if not success then
print("Http Request failed:", message)
end
And I think that where I need help here is how to post back the infomation to my game in PHP. This is my current PHP Code :
<?php
$servername = "localhost";
$username = "username ";
$password = "password";
$dbname = "dbname ";
// Connect to MySQLI and ask it to throw exceptions
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try{
$conn = new mysqli($servername, $username, $password, $dbname);
}catch(mysqli_sql_exception $e){
print("Database connection error: " . $e->getMessage());
}
// Fetch the payload from Roblox
$payload = json_decode(file_get_contents("php://input"), true);
if (empty($payload)){
print("Payload from Roblox is empty. No data was received.");
}else{
$Request = $payload['Request'];
if (isset($Request)){
// Check if Request Exist
if ($Request == "PlayerInformations"){
$array = array();
$result = $conn->query("SELECT * FROM `Players`");
$count = 0;
while ($row = $result->fetch_assoc()){
$PlayerID=$row['PlayerID'];
$array["$count"] = $PlayerID;
$count++;
}
echo json_encode($array);
}
}else{
print("No Request was received.");
}
}
?>
Can I send a Json file to my game or I can only Read what I Print in PHP from the response.Body ?
Right now in my Roblox Console this is what I get
Status code: 200 OK
Response body:
["Player_1874241968","Player_1234"]