Unity3D通过PHP对MySQL执行增、删、改、查

    xiaoxiao2021-03-25  303

    简介

    我们使用Unity3D做数据库相关操作,通常最简便的是使用php做连接。所幸php和MySQL都不是这么难掌握,所以我们的前端应该尽可能了解这方面的知识。本文使用一个游戏分数提交的案例,介绍通过php对MySQL执行增、删、改、查分数的方法。在php脚本中会使用SQL语句,需要有一定掌握。

    开始

    1. MySQL部分。

    使用WampServer快速搭建服务端,新建数据库"simpledata",并建一张表"highscores",设置字段如下表;

    idnamescore1Anna1002Pharah200

    2. PHP部分

    在Wamp默认根目录(/www)中新建addscore.php;

    在php中使用action,可以很好的对应到C#的函数。

    error_reporting(E_ALL ^ E_DEPRECATED); 则可以帮助我们清除冗余的异常,使Unity Console界面变得干净。

    <?php error_reporting(E_ALL ^ E_DEPRECATED); mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error()); mysql_select_db("simpledata"); //增 if($_REQUEST['action']=="submit_highscore") { $name = $_REQUEST['name']; $score = $_REQUEST['score']; $query = "INSERT INTO `highscores` (`name`,`score`) VALUES ('$name','$score')"; mysql_query($query); echo "Insert " . $name . " " . $score; } //删,全部 if($_REQUEST['action']=="delete_all_highscore") { $query = "DELETE FROM `highscores`"; mysql_query($query); echo "Delete All!"; } //删,指定 if($_REQUEST['action']=="delete_highscore") { $name = $_REQUEST['name']; $query = "DELETE FROM `highscores` WHERE `name` = '$name'"; mysql_query($query) or die(mysql_error()); echo "Delete " . $name; } //改,指定 if($_REQUEST['action']=="update_highscore") { $name = $_REQUEST['name']; $score = $_REQUEST['score']; $query = "UPDATE `highscores` SET `score` = '$score' WHERE `name` = '$name'"; mysql_query($query) or die(mysql_error()); echo "Update " . $name . " " . $score; } //查 if($_REQUEST['action']=="show_highscore") { $query = "SELECT * FROM `highscores` ORDER BY `score` DESC"; $result = mysql_query($query); while($array = mysql_fetch_array($result)) { echo $array['name']."</next>"; echo $array['score']."</next>"; } } ?>

    3. Unity部分。

    全部以表单方式提交POST,对照php中的action,传对应的参数。 using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine; public class phpMySQL : MonoBehaviour { string url = "http://localhost/addscore"; void Start() { StartCoroutine(submit_highscore("tom", 100)); //StartCoroutine(delete_highscore("tom")); //StartCoroutine(delete_all_highscore()); StartCoroutine(update_highscore("tom", 233)); StartCoroutine(show_highscore()); } //增 IEnumerator submit_highscore(string player_name, int player_score) { WWWForm form = new WWWForm(); form.AddField("action", "submit_highscore"); form.AddField("name", player_name); form.AddField("score", player_score); WWW www = new WWW(url, form); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log(www.error); } Debug.Log(www.text); } //删,全部 IEnumerator delete_all_highscore() { WWWForm form = new WWWForm(); form.AddField("action", "delete_highscore"); WWW www = new WWW(url, form); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log(www.error); } Debug.Log(www.text); } //删,指定 IEnumerator delete_highscore(string player_name) { WWWForm form = new WWWForm(); form.AddField("action", "delete_highscore"); form.AddField("name", player_name); WWW www = new WWW(url, form); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log(www.error); } Debug.Log(www.text); } //改,指定 IEnumerator update_highscore(string player_name, int player_score) { WWWForm form = new WWWForm(); form.AddField("action", "update_highscore"); form.AddField("name", player_name); form.AddField("score", player_score); WWW www = new WWW(url, form); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log(www.error); } Debug.Log(www.text); } //查 IEnumerator show_highscore() { WWWForm form = new WWWForm(); form.AddField("action", "show_highscore"); WWW www = new WWW(url, form); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log(www.error); } Debug.Log(www.text); var received_data = Regex.Split(www.data, "</next>"); int scores = (received_data.Length - 1) / 2; for (int i = 0; i < scores; i++) { print("Name: " + received_data[2 * i] + " Score: " + received_data[2 * i + 1]); } } }

    转载请注明原文地址: https://ju.6miu.com/read-128.html

    最新回复(0)