借鉴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();
}