標籤:style blog http color os io 檔案 資料
1,PHP登入代碼【login.php】
完整代碼如下
<?php$name=$_POST[‘pname‘];$password=$_POST[‘ppwd‘];if ($name && $password){$conn=mysql_connect("xxxxxxxx","Mysql使用者名稱","MYsql密碼") or die("SomeConstrError");mysql_select_db("getfh_15188775_linmdb");mysql_query("set names utf8");$sql="SELECT * FROM `getfh_15188775_linmdb`.`u_table` WHERE pname = ‘$name‘ and ppwd=‘$password‘"; $res = mysql_query($sql); $rows=mysql_num_rows($res);mysql_query($sql); if($rows){ echo "Login--okokok--"; } else{ echo "error--1--"; }}else { echo "error--2--";}?>
上傳到虛擬機器主機伺服器,代碼很簡單,不懂的自己百度PHP基礎,網上一堆
2,Unity3D登入代碼【Login.cs】
代碼如下
using UnityEngine;using System.Collections;public class Login : MonoBehaviour { public string Server_url = ""; // Use this for initialization void Start () { Server_url = "http://linm123.getfreehosting.co.uk/login.php"; } // Update is called once per frame void Update () { } public string Pname = ""; public string Ppwd = ""; public bool isClicked; public string TempMessage="--Login-XX--"; void OnGUI () { Pname = GUI.TextField (new Rect (130, 0, 100, 50), Pname); Ppwd = GUI.TextField (new Rect (130, 60, 100, 50), Ppwd); if (isClicked == false) { if (GUI.Button (new Rect (130, 120, 100, 50), "Login")) { isClicked = true; StartCoroutine (RegisterData ()); } } GUI.Label( new Rect (130, 180, 150, 50), TempMessage ); } WWWForm form; WWW logindownload; IEnumerator RegisterData () { form = new WWWForm (); form.AddField ("pname", Pname); form.AddField ("ppwd", Ppwd); // Create a download object //建立一個下載對象 logindownload = new WWW (Server_url, form); // Wait until the download is done //等待直到下載完成 yield return logindownload; Debug.Log ("LoginOK - - " + logindownload.text); TempMessage = logindownload.text; isClicked = false; }}
運行測試
輸入使用者名稱 admin
密碼 123
點擊登入
伺服器給我們相應 Login-okokok-了 ,證明帳號密碼正確
下面我們再來看登入並讀取資料
3,PHP登入並迴圈讀取表中所有資料代碼【logingetdata.php】
完整代碼如下
<?php$name=$_POST[‘pname‘];$password=$_POST[‘ppwd‘];if ($name && $password){$conn=mysql_connect("XXXXMYSQL伺服器位址","gMysql帳號","MYsql密碼c") or die("SomeConstrError");mysql_select_db("getfh_15188775_linmdb");mysql_query("set names utf8");$sql="SELECT * FROM `getfh_15188775_linmdb`.`u_table` WHERE pname = ‘$name‘ and ppwd=‘$password‘"; $res = mysql_query($sql); $rows=mysql_num_rows($res);mysql_query($sql); if($rows){ /* echo "Login--okokok--"; */$dbh = @mysql_connect("XXXXMYSQL伺服器位址","gMysql帳號","MYsql密碼c");
if(!$dbh) { die("error"); }
@mysql_select_db("getfh_15188775_linmdb", $dbh);
$q = "SELECT * FROM u_table";
$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_row($rs)) {
echo "name--:".$row[0]."--pwd--:".$row[1]."\r\n--- "; }
/*ssssssend*/
}
else
{ echo "error--1--"; } }
else { echo "error--2--"; }
?>
代碼原理就是先檢查使用者名稱密碼是否正確,如果進入到正確的 if語句裡面,再檢索資料庫表中所有資料,迴圈所有的資料傳回
上傳該檔案到你的虛擬機器主機PHP伺服器
4,Unity3D登入並讀取所有資料代碼【LoginGetAllData.cs】
using UnityEngine;using System.Collections;public class LoginGetAllData : MonoBehaviour { public string Server_url = ""; // Use this for initialization void Start () { Server_url = "http://linm123.getfreehosting.co.uk/logingetdata.php"; } // Update is called once per frame void Update () { } public string Pname = ""; public string Ppwd = ""; public bool isClicked; public string TempMessage="--GetAllData-XX--"; void OnGUI () { Pname = GUI.TextField (new Rect (310, 0, 100, 50), Pname); Ppwd = GUI.TextField (new Rect (310, 60, 100, 50), Ppwd); if (isClicked == false) { if (GUI.Button (new Rect (310, 120, 100, 50), "LoginAllData")) { isClicked = true; StartCoroutine (RegisterData ()); } } GUI.TextArea( new Rect (50, 220, 450, 200), TempMessage ); } WWWForm form; WWW logindownload; IEnumerator RegisterData () { form = new WWWForm (); form.AddField ("pname", Pname); form.AddField ("ppwd", Ppwd); // Create a download object //建立一個下載對象 logindownload = new WWW (Server_url, form); // Wait until the download is done //等待直到下載完成 yield return logindownload; Debug.Log ("GetOK - - " + logindownload.text); TempMessage = logindownload.text; isClicked = false; }}
下面我們測試一下
輸入帳號 admin
密碼 123
點擊 LoginAllData
成功的讀取出了所有的資料
我們發布成WebPlayer下,運行也是沒有問題的
QQ:2360450496
我是林夢,愛生活,愛Coding,愛咖啡!
個人網站:http://wai2dance.s1.jutuo.net/
歡迎與我交流技術,交流心得!