I am trying to fetch data from a database with UnityWebRequest.
This is how I’ve set up my function inside of Unity:
public string[] comments;
public string requestUrl = "http://creativiii.com/architect/data.php?action=retrieve&model_nameGet=test";
public IEnumerator Retrieve()
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(requestUrl))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError)
{
Debug.Log("Error: " + webRequest.error);
}
else
{
Debug.Log("Received: " + webRequest.downloadHandler.text);
string fulldata = webRequest.downloadHandler.text;
comments = fulldata.Split(new string[] { "
" }, StringSplitOptions.None);
Debug.Log(String.Format("There are {0} comments.", comments.Length));
foreach (string comment in comments)
{
Debug.Log(comment);
}
}
}
}
And this is how the data.php file is set up:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($action_type == "retrieve") {
$sql = " SELECT * FROM `ar_comments` WHERE `model_name` = '".$model_name_get."'";
$result = $conn->query($sql);
$push = "INSERT INTO `ar_comments`(`model_name`,`comment`,`position_x`,`position_y`,`position_z`) VALUES ('".$model_name."','".$comment."','".$position_x."','".$position_y."','".$position_z."')";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Comment: " . $row["comment"]. " - Position: " . $row["position_x"]. ", " . $row["position_y"]. ", " . $row["position_z"]. "
";
}
} else {
echo "0 results";
}
}
If I visit the URL from the first script it will correctly return the data I have requested.
However, If I request it from Unity, I will only receive:
0 results
Why is this happening? Is there something wrong with my C# code?