文档库 最新最全的文档下载
当前位置:文档库 › 用Unity利用PHP连接MySQL数据库

用Unity利用PHP连接MySQL数据库

我的工具是Unity+WampSever
首先在phpadmin端建立一个数据库,名字为test
个人的端口可能不同,我装了IIS,所以端口改为了8080
附上php源代码
addscore.php





$db = mysql_connect("localhost","root","") or die('Cant not connect :' .mysql_error()) ;
mysql_select_db('wayneyi') or die('Cant connect into database') ;
$name=mysql_real_escape_string($_GET['name'],$db);
$score=mysql_real_escape_string($_GET['score'],$db);


$sql="insert into scores values (null,'$name','$score');";


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


?>

这是php文件,放入www文件目录
然后就是c#代码
using UnityEngine;
using System.Collections;


public class Downcsharp : MonoBehaviour {




public string strurl = \"http://localhost:8080/addscore.php?\";
static string name = \"\";
int fenshu = 21;
void OnGUI()
{
name = GUI.TextField(new Rect(40, 40, 100, 40), name, 25);
if (GUI.Button(new Rect(40, 100, 100, 40), \"Sumbit\"))
{
StartCoroutine(test(name, fenshu));

}
}
IEnumerator test(string name, int id)
{

string hc = strurl + \"name=\" + www.EscapeURL(name) + \";score\" + id;
WWW hspost = new WWW(hc);
yield return hspost;
if (hspost.error != null)
{
print(\"error with:\" + hspost.error);
}
}

}

附加到照相机上

相关文档
相关文档 最新文档