i am new in php i want to upload player score and get the highest score from the server but i am geting error could not resolve host this is my php script
<?php //Change User and PW $db = mysql_connect('localhost', 'prateek', '*****') or die('Could not connect: ' . mysql_error()); //Change DBname mysql_select_db('u024501_high') 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); $uniqueID = mysql_real_escape_string($_GET['uniqueID'], $db); $hash = $_GET['hash']; $secretKey="123456789"; # 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 = REPLACE INTO scores( '$uniqueID', '$name', '$score' ); //REPLACE INTO scores( id, name, score, uniqueID ) VALUES ( 1, 'Testname', 100, 'A1' ) $query = "replace into scores values ('$uniqueID', '$name', '$score');"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); } ?>whats your code for sending the score?
have you set proper server url there?
yes i am trying to send score and user name to server
url is proper and i am using proper username password db name but when i am trying to use that url from browser it shows error
Fatal error: Uncaught Error: Call to undefined function mysql_connect()
yes i am trying to send score and user name to server
url is proper and i am using proper username password db name but when i am trying to use that url from browser it shows error
Fatal error: Uncaught Error: Call to undefined function mysql_connect()
what kind of server you have running?
right now i update my php code to
<?php //Change User and PW $servername = "localhost"; $username = "id1895_prateek"; $password = "fdsfsdg"; try { $conn = new PDO("mysql:host=$servername;dbname=id1895_highscore", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } // 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); $uniqueID = mysql_real_escape_string($_GET['uniqueID'], $db); $hash = $_GET['hash']; $secretKey="123456789"; # 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 = REPLACE INTO scores( '$uniqueID', '$name', '$score' ); //REPLACE INTO scores( id, name, score, uniqueID ) VALUES ( 1, 'Testname', 100, 'A1' ) $query = "replace into scores values ('$uniqueID', '$name', '$score');"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); } ?>now i am able to connect to server as it showing Connected successfully but now i am geting error of Fatal error: Uncaught Error: Call to undefined function mysql_real_escape_string()
looks like php7 had quite many changes, if your server is running that,
http://stackoverflow.com/a/34579855/5452781
thanks for telling that i changed my php script now there is no error in php files but i am getting same problem there was an error in posting highscore
this is my php script & database image
An error has ocurred.
', $e->getMessage() ,''; } if(isset($_GET["name"&&"scores"])){ $realHash = md5($_GET['name'] . $_GET['score'] . $secretKey); } if(isset($_GET["hash"])){ if($realHash == $hash) { $sth = $dbh->prepare('INSERT INTO scores VALUES (null, :name, :score)'); try { $sth->execute($_GET); } catch(Exception $e) { echo '
An error has ocurred.
', $e->getMessage() ,''; } } } ?>
whats the actual error message?
can you try submitting the score with browser manually (maybe disable hash check during that)
yourserver.com…
i edit my script to is it correct?
<?php // Configuration $hostname = 'localhost'; $username = 'prateek'; $password = '12345'; $database = 'high'; $secretKey = "123456789"; // Change this value to match the value stored in the client javascript below try { $dbh = new PDO('mysql:host='. $hostname .';dbname='. $database, $username, $password); } catch(PDOException $e) { echo 'An error has ocurred.
', $e->getMessage() ,''; } $realHash = md5($name=isset($_GET['name']) .$score=isset( $_GET['score']) . $secretKey); if($realHash == $hash=isset($_GET['hash'])) { $sth = $dbh->prepare('INSERT INTO scores VALUES (:uniqueID, :name, :score)'); try { $sth->execute($_GET); } catch(Exception $e) { echo '
An error has ocurred.
', $e->getMessage() ,''; } } ?>
this is my HSControl script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class HSController : MonoBehaviour
{
private static HSController instance6;
public static HSController Instance
{
get { return instance6; }
}
void Awake() {
DontDestroyOnLoad (gameObject);
// If no Player ever existed, we are it.
if (instance6 == null)
instance6 = this;
// If one already exist, it’s because it came from another level.
else if (instance6 != this) {
Destroy (gameObject);
return;
}
}
void Start(){
startGetScores ();
startPostScores ();
//
//HSController.Instance.startGetScores ();
}
private string secretKey = “1234546789”; // Edit this value and make sure it’s the same as the one stored on the server
string addScoreURL = “gunshoots.000webhostapp.com/addscore.php?”; //be sure to add a ? to your url
string highscoreURL = “gunshoots.000webhostapp.com/display.php”;
//for testing
public string uniqueID;
public string name3;
int score;
public string[ ] onlineHighscore;
public void startGetScores()
{
StartCoroutine(GetScores());
}
public void startPostScores()
{
StartCoroutine(PostScores());
}
//set actual values before posting score
public void updateOnlineHighscoreData()
{
// uniqueID,name3 and score will get the actual value before posting score
uniqueID = “123456789”; //Replace this TestScript variable into your game-variables
name3 = “Testname”;
score = 1000;
}
public string Md5Sum(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[ ] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[ ] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string hashString = “”;
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes*, 16).PadLeft(2, ‘0’);*
}
return hashString.PadLeft(32, ‘0’);
}
// remember to use StartCoroutine when calling this function!
IEnumerator PostScores()
{
updateOnlineHighscoreData ();
//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.
string hash = Md5Sum(name3 + score + secretKey);
//string post_url = addScoreURL + “name=” + WWW.EscapeURL(name) + “&score=” + score + “&hash=” + hash;
string post_url = addScoreURL + “uniqueID=” + uniqueID+ “&name=” + WWW.EscapeURL (name3) + “&score=” + score+ “&hash=” + hash;
Debug.Log ("post url " + post_url);
// Post the URL to the site and create a download object to get the result.
WWW hs_post = new WWW(“http://”+post_url);
yield return hs_post; // Wait until the download is done
if (hs_post.error != null)
{
print("There was an error posting the high score: " + hs_post.error);
}
}
// Get the scores from the MySQL DB to display in a GUIText.
// remember to use StartCoroutine when calling this function!
IEnumerator GetScores()
{
Scrolllist.Instance.loading = true;
WWW hs_get = new WWW(“http://”+highscoreURL);
yield return hs_get;
if (hs_get.error != null)
{
Debug.Log("There was an error getting the high score: " + hs_get.error);
}
else
{
Debug.Log(“sussess”);
//Change .text into string to use Substring and Split
string help = hs_get.text;
//help= help.Substring(5, hs_get.text.Length-5);
//200 is maximum length of highscore - 100 Positions (name+score)
onlineHighscore = help.Split(“;”[0]);
}
Scrolllist.Instance.loading = false;
Scrolllist.Instance.getScrollEntrys ();
}
}
