Hi, i’m new to MySQL system. I have to store in a database:
Username;
Score;
Resources.
For send username, score and resources i don’t have any problems but whene i try to get resources from database i have this error:
Bad request!
Your browser (or proxy) sent a request that
this server could not understand.
If you think this is a server error, please contact
the webmaster .
Error 400
localhost
Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1
UnityEngine.Debug:Log(Object)
c__Iterator1:MoveNext() (at Assets/Scripts/DataBase/MySQL/DBLoader.cs:67)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
This is my php file:
<?php
$servername = "host";
$username = "name";
$password = "";
$dbname = "game_name";
$coinsname = (isset($_REQUEST['coinsnameGet']) ? $_REQUEST['coinsnameGet'] : null);
//Create Connection
$connection = new mysqli($servername, $username, $password, $dbname);
//Check Connection
if(!$connection) {
die("Connection Failed. " . mysqli_connect_error());
}
$sql = "SELECT Coins FROM score WHERE Name = '" . $coinsname . "'";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($result, MYSQLI_BOTH) {
echo $row['Coins'];
}
?>
I have to check if name is == to local database name then get resources.
The local db is made with sqlite.
And this is my connected class:
private DataBase dbLocal;
WWW userData;
WWW scoreData;
WWW coinsData;
public Text Coins;
private string[] dbUsers;
private string[] dbScores;
private string dbCoins;
private string postCoinsData = "http://host/game_name/UsercoinsData.php";
public GameObject scorePrefab;
public Transform scoreParent;
public GameObject rankValue;
public GameObject nameValue;
public GameObject scoreValue;
// Use this for initialization
IEnumerator Start () {
dbLocal = (DataBase)FindObjectOfType(typeof(DataBase));
userData = new WWW("http://host/game_name/UsernameData.php");
scoreData = new WWW("http://host/game_name/UserscoreData.php");
yield return userData;
yield return scoreData;
string textUserData = userData.text;
dbUsers = textUserData.Split(';');
string textScoreData = scoreData.text;
dbScores = textScoreData.Split(';');
scoreParent.transform.localPosition = new Vector3(0, 0, 0);
dbLocal.Connection();
StartCoroutine(SetPlayerName());
GenerateScore();
}
IEnumerator SetPlayerName()
{
string setName = postCoinsData + "coinsnameGet = " + WWW.EscapeURL(dbLocal.GetName());
WWW dataCoins = new WWW(setName);
Debug.Log(dbLocal.GetName());
yield return dataCoins;
string textDataCoins = dataCoins.text;
Coins.text = textDataCoins;
Debug.Log(Coins.text); //This log the error
}
Thanks for any helps!
johne5
February 28, 2017, 4:27am
3
does the php work all by it’s self. will it echo the results?I can send you my scripts if you need something to copy. I used form post to get my scripts working. once i had the php working, then I knew the issue was with unity scripts.
Kiwasi
February 28, 2017, 7:41am
4
What @johne5 said. You need to test each layer separately.
Start with the database. Does the information actually exist? Does the login have the right persmissions to access it.
Then try your PHP. Can you echo the correct result in a browser?
Then try in Unity. Start by just debugging the raw output from the WWW. Then move to trying to parse it.
Also note your PHP is vulnerable to SQL injection. You don’t want this.
Actually in the browser I get this output:
Parse error: syntax error, unexpected ‘echo’ (T_ECHO) in C: \ xampp \ htdocs \ infinity_launch \ UsercoinsData.php on line 22
But I do not understand why. (I’m new in php).
Kiwasi
February 28, 2017, 8:25am
6
Trese759:
Actually in the browser I get this output:
Parse error: syntax error, unexpected ‘echo’ (T_ECHO) in C: \ xampp \ htdocs \ infinity_launch \ UsercoinsData.php on line 22
But I do not understand why. (I’m new in php).
Line 21 is missing a closing bracket.
Obviously… A typical stupid error… Thx for this, but the problem in Unity persist.
There is a method for connect unity game with an server side database without using php?
If yes, can anyone indicate how or some tutorials?
Can you make a simple example? Thx.
From MySQL DB i get the player and the score and i can see in game.
Kiwasi
February 28, 2017, 9:21am
8
Really you can do a WWW to any web endpoint. And then you can parse the response back in Unity. Unity doesn’t actually care where you send it. So you can build your server in any language you like.
You are probably better off looking at some generic web programming tutorials.
So does the php work now? Do you get a meaningful response if you use the browser?
In the browser i now see a blank page, probably becouse the $coinsname variable it’s store in a local database made with sqlite. Essentially when game launch:
public GameObject username //InputField
Start() {
if(sqliteDB.GetName() == null) {
username.SetActive(true);
}
else if(sqliteDB.GetName() != null) {
username.SetActive(false);
}
}
This is for store name in MySQL DB once only. Can make this the error? The best way for make this is store the player name in a game variable instead on the local DB?
Another test in php file:
If i change this:
$sql = "SELECT Coins FROM score WHERE Name = '" . $coinsname . "'";
with this:
$sql = "SELECT Coins FROM score WHERE Name = 'PlayerName'";
In brower i see the score.
Kiwasi
March 2, 2017, 9:29am
14
Trese759:
There isn’t a solution?
There is. We just have very few experts in php and Unity who hang out on the forums.
Trese759:
Another test in php file:
If i change this:
$sql = "SELECT Coins FROM score WHERE Name = '" . $coinsname . "'";
with this:
$sql = "SELECT Coins FROM score WHERE Name = 'PlayerName'";
In brower i see the score.
Which tells me your php is still wrong. Can you simply get the php to echo the variable you pass in via the URL?
Solved, the error was there:
I change this:
string setName = postCoinsData + "coinsnameGet = " + WWW.EscapeURL(dbLocal.GetName());
with this:
string setName = postCoinsData + "?coinsnameGet=" + WWW.EscapeURL(dbLocal.GetName());
Solution by Fredrik (StackOverflow):
which will look something like:
http://host/game_name/UsercoinsData.phpcoinsnameGet = PlayerName
but should look like
http://host/game_name/UsercoinsData.php?coinsnameGet=PlayerName
Thank’s all.