PHP部分:
addscore.php:
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
// 假定数据库用户名:root,密码:123456,数据库:test
$con=mysqli_connect("localhost","root","123456","test");
if (mysqli_connect_errno($con))
{
echo "连接 MySQL 失败: " . mysqli_connect_error();
}
//增
if($_REQUEST['action']=="submit_highscore")
{
$name = $_REQUEST['name'];
$score = $_REQUEST['score'];
$query = "INSERT INTO `tb1` (`userid`,`password`) VALUES ('$name','$score')";
mysqli_query($con,$query);
echo "Insert " . $name . " " . $score;
}
//删,全部
if($_REQUEST['action']=="delete_all_highscore")
{
$query = "DELETE FROM `tb1`";
mysqli_query($con,$query);
echo "Delete All!";
}
//删,指定
if($_REQUEST['action']=="delete_highscore")
{
$name = $_REQUEST['name'];
$query = "DELETE FROM `tb1` WHERE `userid` = '$name'";
mysqli_query($con,$query) or die(mysqli_error());
echo "Delete " . $name;
}
//改,指定
if($_REQUEST['action']=="update_highscore")
{
$name = $_REQUEST['name'];
$score = $_REQUEST['score'];
$query = "UPDATE `tb1` SET `password` = '$score' WHERE `userid` = '$name'";
mysqli_query($con,$query) or die(mysqli_error());
echo "Update " . $name . " " . $score;
}
//查
if($_REQUEST['action']=="show_highscore")
{
$query = "SELECT * FROM `tb1` ORDER BY `userid` DESC";
$result = mysqli_query($con,$query);
while($array = mysqli_fetch_array($result))
{
echo $array['userid']."</next>";
echo $array['password']."</next>";
}
}
unity部分:
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
public class phpMySQL : MonoBehaviour
{
string url = "http://localhost/WE/addscore.php";
void Start()
{
//StartCoroutine(submit_highscore(100005, 123456));
//StartCoroutine(delete_highscore("tom"));
//StartCoroutine(delete_all_highscore());
//StartCoroutine(update_highscore("tom", 233));
//StartCoroutine(show_highscore());
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
//StartCoroutine(submit_highscore(100005, 123456));
//StartCoroutine(delete_highscore(100005));
//StartCoroutine(update_highscore(100001, 456789));
StartCoroutine(show_highscore());
}
}
//增
IEnumerator submit_highscore(int 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(int 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(int 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.text, "</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]);
}
}
}