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

7 篇文章 0 订阅
3 篇文章 0 订阅

借鉴https://blog.csdn.net/abcd5711664321/article/details/81012862

本来是被WebGL端链接数据库而苦恼,看到这篇帖子,很是激动,先看看。。。。。测试以后果然很棒,给博主点个赞

略有不足的是在Unity报错不明了,下面有解决办法,里面修改添加了一些内容,简单的使用应该是够了

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>";
    }
}
//查寻一条数据
if($_REQUEST['action']=="show_highscore_One")
{
	$ID = $_REQUEST['ID'];
    $query = "SELECT * FROM `tb1` WHERE `userid` ='$ID'";
    $result = mysqli_query($con,$query);
    if (!$result) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
}
    while($array = mysqli_fetch_array($result))
    {
        echo $array['userid']."</next>";
        echo $array['password']."</next>";
        echo $array['name']."</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]);
        }
    }
 //查寻其中一条数据
    IEnumerator show_highscore_One(int id)
    {     
        WWWForm form = new WWWForm();
        form.AddField("action", "show_highscore_One");
        form.AddField("ID", id);
        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] + "Name:" + received_data[2 * i + 2]);
            GameObject.Find("Canvas/Text").transform.GetComponent<Text>().text = "Name: " + received_data[2 * i] + " Score: " + received_data[2 * i + 1] + "Name:" + received_data[2 * i + 2];
        }
    }
}

今天调取数据库数据时,一直报一个错:Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in E:\php

后来在网上找了一个很实用的解决方法:

只需要在php文件中写入这样几行代码,便可以“知错就改”了,这样是为了方便他报的错是什么意思

$result = mysqli_query($con,$sql);   
 
if (!$result) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

山竹炒大蒜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值