1.上网下载一个PHPNOW,文件包非常小,但是功能却很强大(下载链接 http://pan.baidu.com/s/1bnpjwUN)。
2.安装PHPNOW,注意不要有中文路径,安装方法也很简单,解压后双击Init.cmd根据提示做就可以。
3.安装后可以在浏览器上输入:localhost/index.php,浏览器会打印出PHPNOW的相关配置信息。
4.安装Dreamweaver CS6,用于编写php代码。
5.使用phpMyAdmin登录数据库,在浏览器中输入localhost/phpMyAdmin
导入一个数据库:附件中有
添加一个用户:用户名和密码要与unity中的登录名和密码一致:wenjian 、1234
6.Unity代码编写
using UnityEngine;
using System.Collections;
using System.Xml;
using System.IO;
using System.Collections.Generic;
public class ConnectPhpAndMySql : MonoBehaviour {
// Use this for initialization
string url = "http://localhost/ConnectMySql/UnityGetUserMessage.php";
//userdata的数据。
private XmlNodeList xmlList;
private List UserMessage=new List();
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
//登录数据库
if(GUI.Button(new Rect (10, 100, 100, 20), "Login")) {
WWWForm form = new WWWForm();
form.AddField("Flat","Login");
form.AddField("Name", "wenjian");
form.AddField("Password", "1234");
WWW www = new WWW(url, form);
StartCoroutine(LoginPhp(www));
}
//获取内容
if(GUI.Button(new Rect (10, 150, 100, 20), "GetMessage"))
{
WWWForm form = new WWWForm();
form.AddField("Flat","GetMessage");
form.AddField("Name", "wenjian");
form.AddField("Password", "1234");
form.AddField("Start_int", "2");
form.AddField("End_int", "5");
WWW www = new WWW(url, form);
StartCoroutine(GetMessagePhp(www));
}
//插入一行数据
if(GUI.Button(new Rect(10,200,100,20),"InsertMessage"))
{
WWWForm form = new WWWForm();
form.AddField("Flat","InsertMessage");
form.AddField("Name", "wenjian");
form.AddField("Password", "1234");
form.AddField("Message", "Insert,he,wen,jian");
WWW www = new WWW(url, form);
StartCoroutine(InsertMessagePhp(www));
}
//更新一行内容
if(GUI.Button(new Rect(10,250,100,20),"UpdateMessage") )
{
WWWForm form = new WWWForm();
form.AddField("Flat","UpdateMessage");
form.AddField("Name", "wenjian");
form.AddField("Password", "1234");
form.AddField("Id", "3");
form.AddField("Setdata", "lkj");
WWW www = new WWW(url, form);
StartCoroutine(UpdateMessagePhp(www));
}
//删除一行内容
if(GUI.Button(new Rect(10,300,100,20),"DeleteMessage") )
{
WWWForm form = new WWWForm();
form.AddField("Flat","DeleteMessage");
form.AddField("Name", "wenjian");
form.AddField("Password", "1234");
form.AddField("Id", "2");
WWW www = new WWW(url, form);
StartCoroutine(DeleteMessagePhp(www));
}
}
IEnumerator LoginPhp(WWW www)
{
yield return www;
Debug.Log(www.text);
// string[] songNames = www.text.Split(',');
}
//把数据与mxl的形式进行数据分析
IEnumerator GetMessagePhp(WWW www)
{
Debug.Log (Time.time);
yield return www;
Debug.Log(www.text);
XmlDocument xmlDoc = new XmlDocument();
string aa=@" ";
aa += www.text;
Debug.Log(aa);
xmlDoc.LoadXml(www.text);
// root = xmlDoc.SelectSingleNode("myRoot");
xmlList=xmlDoc.SelectNodes("myRoot/Message");
Debug.Log ("UserData.length=" + xmlList.Count);
Debug.Log("------"+xmlList[0].InnerText);
for(int jj=0;jj
7.PHP代码
第一步:成功建立连接! ';
mysql_query("set names utf8;");
$db = 'samples';
mysql_select_db($db) or die('Could not select database ('.$db.') because of : '.mysql_error());
//echo '第二步: 成功连接到 ('.$db.') !';
//echo "\n";
}
function GetMySqlMessage()
{
echo "\n";
$List='userdata';
$Start_int=$_REQUEST['Start_int'];
//echo $Start_int;
$End_int=$_REQUEST['End_int'];
//echo $End_int;
//$query="SELECT * FROM '$List' limit '$start_int','$End_int'";
$query="SELECT * FROM $List limit $Start_int,$End_int";
//echo $query;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$count = 0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$str="";
echo "\t\n";
foreach ($line as $col_value) {
// echo "\t\t$col_value \n";
/* if($col_value==null||$col_value=="")
{
$str=$str."".",";
continue;
} */
$str=$str.$col_value.",";
}
echo $str;
$count++;
echo "\t \n";
}
echo " \n";
}
function InsertMessage()
{
$Message=$_REQUEST['Message'];
//插入一行内容
$result="INSERT INTO userdata (Data1,Data2,Data3,Data4 ) VALUES (";
$arr=explode(',', $Message);
$int=0;
$int_end=count($arr);
echo count($arr);
echo "\n";
for($int=0;$int<$int_end-1;$int++)
{
$result=$result."'".$arr[$int]."'".",";
}
$result=$result."'".$arr[$int_end-1]."'";
$result=$result.")";
echo $result;
echo "\n";
$result="INSERT INTO userdata (Data1,Data2,Data3,Data4 ) VALUES ('$arr[0]','$arr[1]','$arr[2]','$arr[3]')";
echo $result;
echo "\n";
//mysql_query($result)or die('insert error'.mysql_error());
mysql_query($result)or die('insert error'.mysql_error());
}
function UpdateMessage()
{
$Id=$_REQUEST['Id'];
$Setdata=$_REQUEST['Setdata'];
mysql_query("update userdata SET Data1 = '$Setdata' WHERE Id = '$Id' ") or die('set Error: ' . mysql_error());
}
function DeleteMessage()
{
$Id=$_REQUEST['Id'];
//删除 fname='wenjian' and lname='31'
mysql_query("delete from userdata where Id='$Id' ") or die('delete Error: ' . mysql_error());
}
?>
http://download.csdn.net/download/he_wen_jian/8218495#6874737-tsina-1-16206-3048ae643c93bac2c9a415075e9789bc