Hello,
So, I’m working on a score board that are stored in an SQL database. All I want to do, is to grab two variables from this database:
Name | Score
So, Unity(JS) > PhP > SQL > PhP > Unity(JS).
This is my script for Unity that connects to my PHP script on my server:
private var highscore_url = "http://MyWebSite.com/game/connent.php";
var playerName = "alex"; // to be an array??
var playerScore = -1;
function Start() {
var form = new WWWForm();
form.AddField( "PHPplayerName", playerName );
var w = WWW(highscore_url, form); //here we create a var called 'w' and we sync with our URL and the form
yield w; //we wait for the form to check the PHP file, so our game dont just hang
if (w.error != null) {
print(w.error); //if there is an error, tell us
} else {
print("Test ok");
formText = w.text; //here we return the data our PHP told us
w.Dispose(); //clear our form in game
}
}
var formText = "";
private var textrect = Rect (10, 150, 500, 500);
function OnGUI(){
GUI.TextArea( textrect, formText );
}
So, what I want my PHP to do (for now) - is to retrieve the names from SQL and give them all to Unity to put in an array of some kind. Here is my PHP script (I know – Unity forum only!) - connent.php:
<?php
// Connect to database
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL"."
";
mysql_select_db(“database”) or die (mysql_error());
echo "Connected to Database
“.”
";
$playerName = ($_POST["PHPplayerName"]);
$score = ('score');
$query = "SELECT * FROM highscores";
$fetch = "SELECT * FROM highscores ORDER BY score desc LIMIT 10";
$result = mysql_query($fetch) or die (mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['player']." - ".$row['score'];
echo "
";
$playerName = $row[‘player’];
}
?>