insert values into mySQL databases

I would like to insert data into mySQL. How can i go about doing this?? I have read up all about php/WWW form but i find it hard to do one. Any kind soul willing to providing some sample code snippet??

Where is the MySQL database located?

Are you talking about MySQL or SQLite? You can't have MySQL packed with your game.

3 Answers

3

I have used MySQL and SQLite with Unity3d. From my experience, I would highly suggest using SQLite unless you have a feature in MySQL that isn’t available in SQLite. That said, in order to have SQLite working in an .exe file, you have to include sqlite3.dll file in the plugins directory during build time. You can download the dll for your system on the SQLite website.

If you still want to use MySQL, you can follow the tutorial at http://www.freewebmasterhelp.com/tutorials/phpmysql

sorry to post to an old question, but to anybody coming across this, and to the original poster as well (should they re-read this), before you indulge in that website (may be good for general knowledge) I highly recommend reading through this article as well if you are going to be using MySQL.

http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html

and another site for PDO

http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

first create a database and create a php code(register.php) and place it in you wamp

<?php
$username=$_REQUEST['username'];
$password=$_REQUEST['password'];
 $database = mysql_connect('localhost', 'root', 'amma') or die('Failed to connect: ' . mysql_error());
        mysql_select_db('gameuser') or die('Failed to access database');
		$query="INSERT INTO users(username,password) VALUES('$username','$password');";
		$result=mysql_query($query);
		echo "registered";
		
?>

unity code

	string getscoreCode="http://192.168.173.243/GameDatabase/register.php";
	
	
	public Rect windowRect0 = new Rect(20,20,500,300);
	public Rect windowRect1 = new Rect(20,350,500,0);
	public Rect windowRect2 = new Rect(20,450,500,0);
	
	public static string username="";
	public static string password="";
	public static string result="";
	public string scoreboard="";
	public string highscore="";
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	void OnGUI() {
		windowRect0 = GUI.Window(0, windowRect0, LeaderBoard, "SCORE ENTRY BOARD");
		if(GUI.Button (new Rect (400, 450, 150, 50), "add"))
		{
			Application.LoadLevel("ScoreBoardDisplay");
		}
	}
	void LeaderBoard(int windowID) {
		GUI.Label (new Rect (140, 40, 150, 100), "Enter the Players name");
		username = GUI.TextField (new Rect (25, 60, 375, 30), username);
		GUI.Label (new Rect (140, 92, 150, 100), "Enter the password");
		password= GUI.TextField(new Rect (25, 115, 375, 30), password);
		if (GUI.Button (new Rect (150, 160, 175, 50), "Save the Score"))
			StartCoroutine(handleEnterScore(username,password));//
		GUI.Label (new Rect (55, 222, 250, 100),result);


		
	}
	IEnumerator  handleEnterScore(string user,string pass)
	{
		
		string register_url = getscoreCode + "?username=" + user + "&password=" + pass;
		WWW entereddata = new WWW (register_url);
		yield return entereddata;
		if(entereddata.text == "registered")
		{
			result=" correctly";
		}
		else{
			result="incorrectly";
		}
		username = "";
		password= "";
	}
	
}