Unity Mysql Connection

I’ve am working on a PHPMyAdmin connection in unity. I want to control the query in my unity script and then send it to php to run it. Then in PHP I echo the information back to unity where I put it in an array to work with. The problem is that the query I send to the PHP script changes when it sends to the URL. Is there a way to send variables to php from unity with the regular _POST method instead of the _GET method so the URL wont get involved?

Thanks in advance,

Tim

ok i got it with a form in unity but i cannot use a where clause because then whatever word i type in after WHERE is stated as not known in unity…

Seems like you’re mixing Unity, PHP and MySQL out of order.
If you’re trying to talk to MySQL direct from Unity, then PHP would be cut out altogether.
More likely scenario would be that you’re talking to PHP from Unity, and PHP is talking to MySQL. In this case you wouldn’t have any SQL in your Unity client. Can you clarify the usage?

Yes sorry you’re right; unity communicates with a php script on the server and the php script sends the query i make in unity to the database. The problem is that the query i make in unity is incorrect according to the error that php gives back. That error sais that whatever word i put after WHERE in de query, is not known.

If i put an integer after the WHERE clause i works perfectly so it’s only with strings i want to search in the database. I have tried several types of double quotes and single quotes but it does not improve the situation.

I saw a function someone made for communicating to mysql via a database that worked for people that were at that post. I’ve tried using that function but the same problem remains.

I hope this clarifies the question.

Thanks in advance.

Seems that my question is not really clear yet so here’s some of my code and errors (should have done it the first post, I know :slight_smile:

In Unity I make a query and send it to my PHP page using a form.

var GetSkillInfoUrl="http://www.domain.com/myphpscript.php";

Database("SELECT id,username,email FROM players WHERE id=265"); //This would work perfectly
Database("SELECT id,username,email FROM players WHERE name='harrysolomon'"); //this would fail

public function Database(Query) 
{
	var form : WWWForm = new WWWForm();
	form.AddField("query",Query);
	var InformationData_get : WWW = new WWW(GetSkillInfoUrl, form);
	yield InformationData_get;
	
	if(InformationData_get.error) 
	{

        print("There was an error getting the skill info: " + InformationData_get.error);

    } 
	else 
	{

        Debug.Log("No Errors selecting skill info.");
        var string1 = InformationData_get.text;
        Debug.Log(string1);
	}

In my PHP page I retrieve my query from Unity using the following code:

$query = $_POST['query'];
echo $query;

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

while($row = mysql_fetch_array($result))
{
	echo "while";
	foreach($row as $value)
	{	
		echo $value;
	}

}

I see now that php gives back an error saying the string i put in, in this case harrysolomon, is not a valid column, but it should search for records instead of columns.

Don’t create the SQL queries in Unity. Instead just send the username and password to PHP and let it create the query and send the results back. You’re opening yourself up to a whole boatload of intrusions and hacks if you use your method.

Yes what Kelso said. It’s best that your client program (the Unity app) knows nothing about your database structure for many reasons. Security, efficiency and maintainability amongst them. Construct the query in php, and just pass the results to Unity.

Probably the way your string or quotes are being passed through is causing a problem… it will be easier to debug directly in php as you’re closer to the db.

Thanks for the advice! I reconsidered my coding and it’s safer ánd easier the way you described. The only problem would be when I want to get a lot of information from a player to be shown to the player in unity. How would I get all that variables into unity? Just as 1 long string with a recurring ‘tag’ so I can split it, or is there an easier way?

Depends how much data you’re transporting and how often - you can make a custom format tailored to your data. An solid option is to encode the data as json. Very easily handled in php, and there are Unity compatible scripts which make decoding it straightforward too. It can handle complex data and does not add excessive amounts of markup. Also, it’s easy to handle with web based javascript and other web platforms, which might be useful for some applications.