Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ForeignGod · Apr 30, 2014 at 11:06 PM · javascriptmysqlphpsqldatabase

Sending values from Unity to PhP to SQL.

Hey, ive currently been fixing around with databases used for highscores. So far im actually able to get information from SQL > PhP > Unity in real time, but i cant seem to send values from unity to the sql database.

I have 2 PhP scripts and one Unityscript

Addingscores (addscores.php)

 <?php
 
  $db = mysql_connect('*******', '******_***', '********') or die('Could not connect: ' . mysql_error()); 
         mysql_select_db("******_***", $db) or die('Could not select database');
 
         // Strings must be escaped to prevent SQL injection attack. 
         $name = mysql_real_escape_string($_GET['name'], $db); 
         $score = mysql_real_escape_string($_GET['score'], $db); 
         $hash = $_GET['hash']; 
 
 
         $secretKey="******"; # Change this value to match the value stored in the client javascript below 
 
 
          $real_hash = md5($name . $score . $secretKey); 
         if($real_hash == $hash) { 
             // Send variables for the MySQL database class. 
             $query = "INSERT INTO scores(CORRECT_COL_NAMES) VALUES ('null', 'name','$score')";
             $results = mysql_query($query, $connection);
         }  
 ?>

There is no errors popping up whatsoever on the website so it has been connected properly.

Displayscores on website(display.php)

 <head> Getting Scores </head>
            

 <head2>______________</head2>
             

 <head3><strong>Name,     Score</strong></head3>
 
 
 <?php
     // Send variables for the MySQL database class.
     $database = mysql_connect('*******', '*******_******', '*******') or die('Could not connect: ' . mysql_error());
     mysql_select_db('*******_***') or die('Could not select database');
  
     $query = "SELECT * FROM `scores` ORDER by `score` DESC LIMIT 5";
     $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  
     $num_results = mysql_num_rows($result);  
  
     for($i = 0; $i < $num_results; $i++)
     {
          echo "  

"; $row = mysql_fetch_array($result); echo $row['name'] . "\t" . $row['score'] . "\n"; } ?> This is the script i use in Unity as my controller

 private var secretKey="*******"; // Edit this value and make sure it's the same as the one stored on the server
 var addScoreUrl="*********************"; //be sure to add a ? to your url
 var highscoreUrl="*********************";    
 var pname : String = "";
 var score : long = 0;
 
 var native_width : float = 1920;
 var native_height : float = 1080;
 
 function Start() {
     getScores();
 }
  
 function postScore(pname, score) {
     //This connects to a server side php script that will add the name and score to a MySQL DB.
     // Supply it with a string representing the players name and the players score.
     var hash=Md5.Md5Sum(pname + score + secretKey); 
  
     var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(pname) + "&score=" + score + "&hash=" + hash;
  
     // Post the URL to the site and create a download object to get the result.
     hs_post = WWW(highscore_url);
     yield hs_post; // Wait until the download is done
     if(hs_post.error) {
         print("There was an error posting the high score: " + hs_post.error);
     }
 }
  
 // Get the scores from the MySQL DB to display in a GUIText.
 function getScores() {
     cookiecounter.status = "Loading Scores";
     hs_get = WWW(highscoreUrl);
     yield hs_get;
  
     if(hs_get.error) {
         print("There was an error getting the high score: " + hs_get.error);
     } else {
         cookiecounter.status = hs_get.text; // this is a GUIText that will display the scores in game.
     }
 }
 
 function OnGUI () {
 
     var rx : float = Screen.width / native_width;
     var ry : float = Screen.height / native_height;
         
     GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (rx, ry, 1)); 
 
 if (GUI.Button(new Rect(900,1000,100,70), "Post Record"))
     {
     pname = cookiecounter.Username;
     score = cookiecounter.tcounter;
     postScore(pname, score);
     }
 if (GUI.Button(new Rect(1000,1000,100,70), "Get Record"))
     {
     getScores();
     }
 }

My SQL Query
http://gyazo.com/38e02531659aa6ba0a6e6c54f552b78a

How it looks ingame(ignore number at top, also if you know how to remove the coding then please help) http://gyazo.com/2b990c50e9049ecdaa4ab120f266883a

Display on website
http://gyazo.com/09e58d3babc0ad7e215f4196cec1210b

As you can see im actually getting the scores from the SQL to the PhP script and then to Unity realtime, but i cannot seem to do it the other way around. Blanked out passwords etc.

If you need more info please tell me. Thanks in advance ^^

Comment
Add comment · Show 2
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 AndyMartin458 · Apr 30, 2014 at 11:15 PM 0
Share

There's so much information here, it is difficult to see what your issue is and where the relevant code is. $$anonymous$$aybe you shouldn't show the code that is working.

avatar image ForeignGod · Apr 30, 2014 at 11:19 PM 0
Share

Sure, i was just wondering if some of it might be interfering with eachother.

The main problem is addingscores, using the postscore function in the unity script and the addscores php script.

 function postScore(pname, score) {
     //This connects to a server side php script that will add the name and score to a $$anonymous$$ySQL DB.
     // Supply it with a string representing the players name and the players score.
     var hash=$$anonymous$$d5.$$anonymous$$d5Sum(pname + score + secret$$anonymous$$ey); 
  
     var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(pname) + "&score=" + score + "&hash=" + hash;
  
     // Post the URL to the site and create a download object to get the result.
     hs_post = WWW(highscore_url);
     yield hs_post; // Wait until the download is done
     if(hs_post.error) {
         print("There was an error posting the high score: " + hs_post.error);
     }
 }

 <?php
  
  $db = mysql_connect('*******', '******_***', '********') or die('Could not connect: ' . mysql_error()); 
         mysql_select_db("******_***", $db) or die('Could not select database');
  
         // Strings must be escaped to prevent SQL injection attack. 
         $name = mysql_real_escape_string($_GET['name'], $db); 
         $score = mysql_real_escape_string($_GET['score'], $db); 
         $hash = $_GET['hash']; 
  
  
         $secret$$anonymous$$ey="******"; # Change this value to match the value stored in the client javascript below 
  
  
          $real_hash = md5($name . $score . $secret$$anonymous$$ey); 
         if($real_hash == $hash) { 
             // Send variables for the $$anonymous$$ySQL database class. 
             $query = "INSERT INTO scores(CORRECT_COL_NA$$anonymous$$ES) VALUES ('null', 'name','$score')";
             $results = mysql_query($query, $connection);
         }  
 ?>

These two, if that narrows it down a bit?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by akguldeniz · May 01, 2014 at 06:56 AM

i think you can create xml file with unity engine and read it with php than write to database

Comment
Add comment · 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

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Register help 0 Answers

HOW Unity store game saves? Are mySQL a valid option to store such data? How about javascript framework or php? 0 Answers

Javascript with php 1 Answer

Server-side generated heightmap 0 Answers

www php send data to mysql 2 Answers


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