首页 > 代码库 > Unity3D_WebPlayer_PHP连接MySQL数据库_3_php登录和读取所有数据
Unity3D_WebPlayer_PHP连接MySQL数据库_3_php登录和读取所有数据
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/
欢迎与我交流技术,交流心得!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。