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

简介

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


开始

1. MySQL部分。

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

idnamescore
1Anna100
2Pharah200


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]);
        }
    }
}


  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值