PHP Wamp Unity 3D communication

Hi All
Me and a small group of developers have come across a huge problem what we are confronted with is, how do we communicate with a php script that is attached to our website. We already have a script that allows us to grab information from the php file (at the moment that is for a login file) but what we want to do know is to add information the php file.
WE have searched around for a while and have not been about to find anything that is directly related to what we want. This is because we have not had much experience with php files, but some html is known in our group.

Any help would be greatly appreciated.

Multiple ways to go about it, but it mostly depends on what you really mean by “add information to php file”.

If you mean actually adding html/php code to the file itself, you can either make the file on one side and send it to the server through FTP, or send data to an other PHP file that will write the files, with all the logic for that on the server side.

The other way I can understand the question is “how do I send data to the PHP files?”, and that can be done by simply calling the correct url, with the data, and this is technically the same as the second way above.

I ask this, because when you talk PHP, and not just HTML, it means that you also have access to a whole data layer to build your pages, from files to databases.

Not certain it helps a lot though, sorry.

Okay so thanks to slay_mithos i can inform you with more usful information. First of all here is my PLP file.

<?php
    $user = $_GET['username'];
    $pass = $_GET['password'];

    $sqlconnection = mysqli_connect("localhost", "root", "", "players");
    if(mysqli_connect_errno()) {
        echo"failed to connect".mysqli_connect_error();
    }

    if(isset($user) && isset($pass)) {
        $query = "SELECT * FROM users WHERE Username = '".$user."' and Password = '".$pass."'";
        $result = mysqli_query($sqlconnection, $query);

        if ($result->num_rows == 0) {
            echo "Nope";
        } else {
            echo "success";
        }
    }
?>

And here is my current code that i use to grab the information from the plp file for your login code.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LoginMenu : MonoBehaviour {

    string loginURL = "http://websitename.net/login.php";

    string username = "";
    string password = "";
    string label = "";

    bool loadlevel = false;


    public Text usernameTXT;
    public Text passwordTXT;
    public Text labelTXT;

    public string SceneName = "InGameMenu";

    public void Login () {
        StartCoroutine (HandleLogin (username, password));
    }


    void Update () {
        username = usernameTXT.text;
        password = passwordTXT.text;

        usernameTXT.color = Color.black;
        passwordTXT.color = Color.black;   
        if(loadlevel == true){
            Application.LoadLevel (SceneName);
        }
    }

    IEnumerator HandleLogin(string username, string password) {
        labelTXT.text = "Checking username and password";
        string loginURL = this.loginURL + "?username=" + username + "&password=" + password;
        WWW loginReader = new WWW (loginURL);
        yield return loginReader;

        if(loginReader.error != null){
            labelTXT.text = "Error connecting to the database server.";
        }else{
            if(loginReader.text == "success"){
                labelTXT.text = "successfully logged in.";
                loadlevel = true;
            }else {
                labelTXT.text = "Invalid username or password";
            }
        }
    }
}

So what i want to know is, how would i go about makeing so i can register a player and have the usernam and password uploaded to plp file?

I would go about it just the same way as your “login”.

A page that makes an INSERT in the database, maybe with a SELECT to check the user doesn’t already exist.
And on your client, a simple UI that sends the data.

It’s basically the same as you did, just writing in the database instead of just reading.

Still, it sounds a bit overly complicated to just connect to a database, but hey, I would tend to make an actual server-side program for that, so it’s not really that much easier in the end, and it requires making that program, when a few simple PHP files seem to be able to do the trick for you.

The only downside to your way is if you need to send a lot of data back and forth, because PHP needs to process the data with each call, and sends a proper page, meaning a lot of unneeded data.

Hmmm this is useful but i kind of need a base script to work off, i’m saying this because i spent 5 hours (until midnight) trying out different methods…

So just to make sure we are on the same page what you are saying is… i need to do some script like this?

register.php

<?php
    $user = $_GET['username'];
    $pass = $_GET['password'];
    $sqlconnection = mysqli_connect("localhost", "root", "", "players");
    if(mysqli_connect_errno()) {
        echo"failed to connect".mysqli_connect_error();
    }
    if(isset($user) && isset($pass)) {
        $query = "INSURT* FROM users WHERE Username = '".$user."' and Password = '".$pass."'";
        $result = mysqli_query($sqlconnection, $query);
        if ($result->num_rows == 0) {
            echo "Nope";
        } else {
            echo "success";
        }
    }
?>

and have this code in unity calling it.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LoginMenu : MonoBehaviour {
   
    string loginURL = "website.name/login.php";
    string registerURL = "website.name/register.php";
   
    string username = "";
    string password = "";
    string label = "";
   
    bool loadlevel = false;
   
   
    public Text usernameTXT;
    public Text passwordTXT;
    public Text labelTXT;
   
    public string SceneName = "InGameMenu";
   
    public void Login () {
        StartCoroutine (HandleLogin (username, password));
    }

    public void Register () {
        StartCoroutine (HandleRegister (username, password));
    }

   
    void Update () {
        username = usernameTXT.text;
        password = passwordTXT.text;
       
        usernameTXT.color = Color.black;
        passwordTXT.color = Color.black; 
        if(loadlevel == true){
            Application.LoadLevel (SceneName);
        }
    }
   
    IEnumerator HandleLogin(string username, string password) {
        labelTXT.text = "Checking username and password";
        string loginURL = this.loginURL + "?username=" + username + "&password=" + password;
        WWW loginReader = new WWW (loginURL);
        yield return loginReader;
       
        if(loginReader.error != null){
            labelTXT.text = "Error connecting to the database server.";
        }else{
            if(loginReader.text == "success"){
                labelTXT.text = "successfully logged in.";
                loadlevel = true;
            }else {
                labelTXT.text = "Invalid username or password";
            }
        }
    }

    IEnumerator HandleRegister(string username, string password) {
        labelTXT.text = "Checking username and password avalability";
        string registerURL = this.registerURL + "?username=" + username + "&password=" + password;
        WWW registerReader = new WWW (registerURL);
        yield return loginReader;
       
        if(registerReader.error != null){
            labelTXT.text = "Error connecting to the database server.";
        }else{
            if(registerReader.text == "success"){
                labelTXT.text = "successfully registered.";
                loadlevel = true;
            }else {
                labelTXT.text = "Could not sent register information";
            }
        }
    }


}

Thanks for all the help so far!!

INSERT INTO your_table VALUES (value1, value2);

A pretty good link for your SQL statements (specifically INSERT here):

You seem pretty close, but if you need PHP and SQL help, google usually has tons of informations, (I found the link by searching “SQL insert”), and it means you are not forced to wait for someone to answer.

First of all thanks to everyone who has been helping us in the plp problem.
This is helpful but i think that it is generally for Java (I only use C#).
So if anyone has any code that adds information to sql data chart could we have a look at it?

Thanks

What are you talking about? he posted a link to sql which is what you needed. Sql is a language used to run queries against a database… its definitely not java.

Im not sure what your problem actually is here? Im guessing its not working? Thats because the sql command you used was incorrect for inserting a record to a table (sql insert)