Hi guys,
Have a little problem when using MySQL through unity. I am trying to receive a string from a (VARCHAR) table in MySQL, and when received, i want to combine it with another string. Each string should be separated by a “,” so for example if I receive a “3” and want to add a “5”, the result should be “3,5”.
I connect through Unity to a .php script on my server which gets the existing string of the table, as seen in the GetUserProjectId(). Afterwards I call the AddToUserProjectId() which takes the existing string “userProjectIdString” and the new string “projectId.ToString()” and forwards them to another .php script.
function GetUserProjectId(){
var URL = "http://getsharp.net/getUserProjectId.php"; //change for your URL
userId = userId+1;
var form = new WWWForm(); //here you create a new form connection
form.AddField( "myform_hash", hash ); //add your hash code to the field myform_hash, check that this variable name is the same as in PHP file
form.AddField( "myform_userId", userId );
var w = WWW(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 {
formText = w.text; //here we return the data our PHP told us
w.Dispose(); //clear our form in game
}
userProjectIdString = formText;
AddToUserProjectId();
}
function AddToUserProjectId(){
var URL = "http://getsharp.net/addToUserProjectId.php"; //change for your URL
var form = new WWWForm(); //here you create a new form connection
form.AddField( "myform_projectIdExisting", userProjectIdString );
form.AddField( "myform_projectIdNew", projectId.ToString() );
form.AddField( "myform_userId", userId );
form.AddField( "myform_hash", hash ); //add your hash code to the field myform_hash, check that this variable name is the same as in PHP file
var w = WWW(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 {
formText = w.text; //here we return the data our PHP told us
w.Dispose(); //clear our form in game
}
print(formText);
}
There is no problem adding a “,” between the two strings in my .php script called “addToUserProjectId.php”, and updating the string to my table, but it is when I want to receive the existing string from “getUserProjectId.php” that the error occurs. The .php script automatically removes the “,” and just gives me a string only with numbers. Below is the .php code that selects the existing string from the database.
$userId = anti_injection_login($_POST["myform_userId"]);
if ($unityHash != $phpHash){
echo "HASH code is different from your game, you infidel.";
} else {
$sql = mysql_query("SELECT projectId FROM users WHERE id='$userId'");
while($result = mysql_fetch_array($sql)) {
$projectId = $result["projectId"];
$data = $projectId;
echo $data;
}
}
And just so I am sure that there is no error when updating the table, below is the code from the .php script where I update my table with the two strings:
$projectIdExisting = anti_injection_login($_POST["myform_projectIdExisting"]); //I use that function to protect against SQL injection
$projectIdNew = anti_injection_login($_POST["myform_projectIdNew"]);
if($projectIdExisting == null){
$idToInsert = $projectIdNew;
}else{
$idToInsert = $projectIdExisting.",".$projectIdNew;
}
$userId = anti_injection_login($_POST["myform_userId"]);
if ($unityHash != $phpHash){
echo "HASH code is different from your game, you infidel.";
} else {
$query = "UPDATE users SET projectId='$idToInsert' WHERE id='$userId'";
mysql_query($query) or die ('Error updating database');
echo"Database updated";
}
Anybody see a problem?
Thanks.