unity连接数据库--可在安卓设备连接(PHP服务器+MySql)

时间:2022-09-23 15:29:07

经测试Windows和安卓设备均可运行
没有任何加密措施
PHP代码是在Apache端运行的,配置MySql服务器时,容许远程登陆
PHP端代码

<?php $myData=mysqli_connect( "数据库地址" ,"登录名" ,"密码" ); if ( mysqli_connect_errno()) { echo "eron"; return; } // 选择数据库 mysqli_query($myData,"set names utf8") ; mysqli_select_db($myData ,"qdm219186353_db" ); // 寻 $userID=$_POST['PlayerID']; $userPWD=$_POST['PlayerPwd']; $sql = "SELECT Id FROM PlayGame where Id=$userID and Pwd='$userPWD' "; $result = mysqli_query($myData,$sql)or die("<br>SQL error!<br/>"); $num_results = mysqli_num_rows($result); echo $num_results; ?>

Unity端代码

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Loading : MonoBehaviour {

    public InputField Id;
    public InputField Pwd;
    string urlSql = "php代码的网络位置,我的是:www.wellwj.com/IsLoadPlayer.php";
    public void isLoad()
    {
        string idText = Id.text;
        string pwdText = Pwd.text;
        StartCoroutine(UploadScore(idText, pwdText));
    }

    IEnumerator UploadScore(string id, string pwd)
    {
        WWWForm ws = new WWWForm();
        ws.AddField("PlayerID", id);
        ws.AddField("PlayerPwd", pwd);
        WWW www = new WWW(urlSql, ws);
        yield return www;
        if (www.text == "1")
        {
            Application.LoadLevel("Yes");
        }
        else
        {
            Application.LoadLevel("No");
        }
    }
}

根据输入的Id和Pwd,搜索数据库中符合条件的数据,有且只有一行时,确认帐号和密码正确
所以判断返回值是否为1
安卓演示(MIUI)
unity连接数据库--可在安卓设备连接(PHP服务器+MySql)
unity连接数据库--可在安卓设备连接(PHP服务器+MySql)
unity连接数据库--可在安卓设备连接(PHP服务器+MySql)


欢迎关注我的微信个人订阅号
unity连接数据库--可在安卓设备连接(PHP服务器+MySql)
每天多学一点0.0