Search Unity

Unity - PHP - MySql question

Discussion in 'Multiplayer' started by kornel-l-varga, Jul 21, 2020.

  1. kornel-l-varga

    kornel-l-varga

    Joined:
    Jan 18, 2013
    Posts:
    43
    Hi

    I am attempting to access a MySQL database from Unity using the UnityWebRequest class.
    The order of connection would be UnityWebRequest>>a PHP file>>MySql database.

    The database is set up with the MySQL workbench application, and a VS script is running to enable a UI and data access through a port. This is basically a web-browser based manipulation interface for the database and it should be independent from the Unity access.
    The whole thing is running on localhost with a local folder as root.

    From Unity I am trying to access a .php file I placed in the localhost's folder system.
    I have tested the path and the access with a simple .txt file and a .html file too and I can access them without a problem, however when trying to access the .php I get a not found error. It is 100% sure that the filename and path is correct.

    I am using the following code :

    Code (CSharp):
    1.         UnityWebRequest r = UnityWebRequest.Get("http://localhost:50693/.php/webtest.php");
    2.        
    3.         yield return r.SendWebRequest();
    4.  
    5.  
    6.         if (r.isNetworkError || r.isHttpError)
    7.         {
    8.             Debug.LogError(r.error);
    9.         }
    10.         else
    11.         {
    12.             Debug.LogError(r.downloadHandler.text);
    13.         }
    and the result is :
    HTTP/1.1 404 Not Found

    When I am using the same code with the same path except for the filename as test.txt or test.html, I am getting the correct contents of the files.

    Any idea what could be the problem?
     
  2. Jirka-Mayer

    Jirka-Mayer

    Joined:
    Mar 18, 2019
    Posts:
    18
    Maybe your http sever (or php processor) does not serve files from unix-hidden folders (folders starting with a dot "." - e.g. "host/.php/..."). Try moving the webtest.php file to a different directory. You also say that you use the same path for .txt and .html and those work fine. So maybe your http server just doesn't know how to process a .php file. If it's an Apache server, you need to install a php extension into it. If it's an Nginx, then you need to refer it to a php-fpm process. Either way I didn't catch what your http server is so cannot help more...

    Anyways, doing all of this by yourself is quite a bit of hassle (as you see). I've just released a tool that lets you build a backend server with a database easily. You can use C# on the server-side and not PHP and also the JSON serialization is completely invisible and transparent :) Check it out, I would like to know what you think of it: https://unisave.cloud/

    Thanks ;)

    EDIT: This article might be relevant to people reading:
    Why not to build game backend server with MySQL and PHP
     
    Last edited: Jul 28, 2023
  3. Barry100

    Barry100

    Joined:
    Nov 12, 2014
    Posts:
    200
    are you running a local php server? Like WAMP or something?
    You cant run PHP files locally otherwise.