Unity3D与PHP对MySQL执行增、删、改、查
借鉴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();
}
Unity3D与PHP对MySQL执行增、删、改、查相关推荐
- Linux技术--mysql数据库增-删-改-查
# mysql 数据库 ## 数据库的操作 ### 五个单位 * 数据库服务器 Linux或者 windows * 数据库 * 数据表 * 数据字段 * 数据行 ### 连接数据库 ``` 1 ...
- 表单的增 删 改 查
django单表操作 增 删 改 查 一.实现:增.删.改.查 1.获取所有数据显示在页面上 model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取 ...
- properties(map)增.删.改.查.遍历
import java.util.Map; import java.util.Properties; import java.util.Set;/*** properties(map)增.删.改.查. ...
- python学生姓名添加删除_python-函数-实现学生管理系统,完成对学员的增,删,改,查和退出学生管理系统。...
实现学生管理系统,完成对学员的增,删,改,查和退出学生管理系统. 要求1:使用一个list用于保存学生的姓名. 要求2:输入0显示所有学员信息,1代表增加,2代表删除,3代表修改,4代表查询,exit ...
- 简单的php数据库操作类代码(增,删,改,查)
数据库操纵基本流程为: 1.连接数据库服务器 2.选择数据库 3.执行SQL语句 4.处理结果集 5.打印操作信息 其中用到的相关函数有 •resource mysql_connect ( [stri ...
- PySpark︱DataFrame操作指南:增/删/改/查/合并/统计与数据处理
笔者最近需要使用pyspark进行数据整理,于是乎给自己整理一份使用指南.pyspark.dataframe跟pandas的差别还是挺大的. 文章目录 1.-------- 查 -------- -- ...
- python_操作MySQL 初解 之__类方法调用并 增-删-改-查
文件一: 调用(sqls文件) 1 # 导入模块 2 import pymysql 3 from sqls import * 4 # 创建类 5 class KaoShi(object): 6 7 # ...
- Python 操作 Elasticsearch 实现 增 删 改 查
Github 地址:https://github.com/elastic/elasticsearch-py/blob/master/docs/index.rst 官网地址:https://elasti ...
- pyRedis - 操作指南:增/删/改/查、管道与发布订阅功能
文章目录 1 redis docker 部署与安装 2 py - redis的使用 2.1 redis的连接 2.2 常规属性查看 2.2.2 关于删除 2.3 STRING 字符串的操作 2.4 H ...
最新文章
- php中address,address.php
- 洛谷 - P4001 [ICPC-Beijing 2006]狼抓兔子(网格图最大流转换为对偶图最短路)
- CodeForces - 1333C Eugene and an array(尺取)
- SVN 集中式版本管理工具
- linux不用命令开启ssh,不用密码也能ssh登陆Linux?
- javascript获取asp.net服务器端控件的值
- C++杂记之this指针
- asp.net GridView手写事件,包括取主键、取值、更新、选择、删除
- .NET 6 Preview 5
- win7 怎么干净删除php,Win7环境下彻底清除VBS病毒的教程
- Android URL Scheme
- 总结了一些很实用值得收藏的站点
- vs201X的TFS(Team Foundation Server) 设置为:不自动签出
- MSVC创建的Qt工程图标设置
- BZOJ 3097: Hash Killer I
- angular 访问图片路径错误
- 阿里VIP会员看腾讯视频?这不是一个意外的选择
- 推荐一款备忘录便签APP简约款安卓版本
- Python数据分析19——matplotlib可视化(一)之线形图
- 影响人生的十部电影(网络流传,作者不详)