Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oliver-jones · Jun 11, 2011 at 09:06 PM · databasephp

PHP - Unity Scoreboard

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"."\n";
 mysql_select_db("database") or die (mysql_error());
 echo "Connected to Database\n"."\n";
 
 $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 "\n";
     $playerName = $row['player'];
 }
 ?>
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Oliver Eberlei · Jun 12, 2011 at 10:32 AM

Since you already get the output from your PHP Script with this code

 formText = w.text;

you can split that text into the various components like this

 string[] playerEntries = formText.Split( '\n' );
 
 foreach( string entry in playerEntries)
 {
     string[] playerData = entry.Split( '-' );
     string playerName = playerData[ 0 ];
     int playerScore = System.Convert.ToInt32( playerData[ 1 ] );
 }

You also have to remove the spaces between the "-" in the PHP output so you can split the string properly

 echo $row['player']."-".$row['score'];
 

And remove the previous echo's from the mysql database.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zwacky · Jun 12, 2011 at 10:39 AM 0
Share

keep in $$anonymous$$d that the splitting doesn't work if the user may enter every kind of characters (including '-').

also, every one who's only a bit familiar with http requests can forge his own by looking at the url you're asking for. an adress like **http://$$anonymous$$yWebSite.com/game/connent.php?PHPPlayerName=me≻ore=5000** is a really easy one to mess with your score board.

there are several solutions for this, one might be adding an additional parameter that's the hash of the other parameters + a secret string in the code.

avatar image oliver-jones · Jun 12, 2011 at 12:05 PM 0
Share

I know I'm asking for too much here - could you please explain this hash security parameter? Cheers

avatar image Oliver Eberlei · Jun 12, 2011 at 12:30 PM 1
Share

For this script this is not neccessary, since you are only getting the highscores from the server.

But if you use a similar script to insert scores into your database you would need some protection against cheaters.

You can see an example of it here http://www.unifycommunity.com/wiki/index.php?title=Server_Side_Highscores

Basically what you do is $$anonymous$$D5() the score together with a secret phrase, and then you do the same in your PHP script and check if the two hases match. If not, someone has tampered with the posted scores

avatar image oliver-jones · Jun 12, 2011 at 07:57 PM 0
Share

Okay - is the code above in JavaScript? And do I put that in an update function? Thanks

avatar image Oliver Eberlei · Jun 12, 2011 at 08:50 PM 0
Share

It's C# and you put it right after formText = w.text; Never used JavaScript in Unity, but translating from JS is not that hard, shouldn't be too bad to convert it to JS.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Using Unity to connect to a Database 3 Answers

A node in a childnode? 1 Answer

unity + php get Authentication 0 Answers

Players online counter 1 Answer

Retrieve a value from a PHP file and print it on the Unity3D console. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges