Unity3D与PHP对MySQL执行增、删、改、查

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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奇大可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值