当我通过 XAMPP 使用 Unity C# 访问 Sqlite 数据库时,我得到了奇怪的结果
I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP
<?php
$servername = "localhost";
$username="root";
$password="";
$dbName="escape_room";
$user_username = $_POST['Input_user'];
$user_password = $_POST['Input_pass'];
$conn = new mysqli($servername, $username, $password, $dbName);
if(!$conn){
die("Cound not Connect: " . mysqli_connect_error());
}
$sql = "SELECT pass FROM escape_room WHERE user = '".$user_username."' ";
$result = mysqli_query($conn, $sql);
//Get the result and confirm login
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
if($row['pass'] == $user_password){
echo "login success";
echo $row['pass'];
}else{
echo "password incorrect";
echo "password is =". $row['pass'];
}
}
}else{
echo "user not found";
echo "password is =". $row['pass'];}
我在 Unity C 脚本中创建了一个 WWWForm,并试图通过服务器 XAMPP 上的 运行 Login.php 获取 Mysqlitedatabase 的值。但是,我无法确认 ID 是否匹配。只有用 html 语言编写的代码才会出现在控制台中。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Object not found!</title>
...
我该怎么办?
感谢您阅读。
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class gamemanager : MonoBehaviour {
[Header("LoginPanel")]
public InputField ID;
public InputField PW;
[Header("CreateAccountPanel")]
public InputField New_ID;
public InputField New_PW;
public string LoginUrI;
// Use this for initialization
void Start () {
LoginUrI = "localhost/escape_room/Login.php";
}
public void LoginBtn()
{
StartCoroutine(LoginToDB(ID, PW));
}
IEnumerator LoginToDB(InputField username, InputField password)
{
Debug.Log(username.text);
Debug.Log(password.text);
WWWForm form = new WWWForm();
form.AddField("Input_user", username.text);
form.AddField("Input_pass", password.text);
WWW webRequest = new WWW(LoginUrI, form);
yield return webRequest;
Debug.Log(webRequest.text);
}
public void CreateAccountBtn()
{
}
}
+添加)
[ 这是我在 Unity C 脚本中实现的。我有一个 InputField,我使用 AddField 方法接受该 inputField 并将其传递给 php。此外,我创建了一个 WWW 对象以允许通过服务器访问 Login.php。最后,我通过 Debug.Log 进行了测试,以查看登录结果的外观。 ]
- 确认您在 xampp 上的路径,以确保这是您的目录路径 C:\xampp\htdocs\escape_room\Login.php
- 您实际上需要先从 unity post 表单,请参阅下面的代码
- 在没有 sql 的情况下单独调试 php 以确保您获得正确的数据。见代码。
WWW形式
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.Networking;
public class gamemanager : MonoBehaviour {
[Header("LoginPanel")]
public InputField ID;
public InputField PW;
[Header("CreateAccountPanel")]
public InputField New_ID;
public InputField New_PW;
string LoginUrI = "http://localhost/escape_room/login.php";
// Use this for initialization
void Start () {
}
public void LoginBtn()
{
StartCoroutine(LoginToDB(ID, PW));
}
IEnumerator LoginToDB(InputField username, InputField password)
{
Debug.Log(username.text);
Debug.Log(password.text);
WWWForm form = new WWWForm();
form.AddField("Input_user", username.text);
form.AddField("Input_pass", password.text);
// Make Post Request
using (var w = UnityWebRequest.Post(LoginUrI, form))
{
yield return w.SendWebRequest();
if (w.isNetworkError || w.isHttpError) {
Debug.Log(w.error);
}
else {
Debug.Log(w.downloadHandler.text);
}
}
}
public void CreateAccountBtn()
{
}
}
测试 php 代码,用这个替换你的 Login.php 以确保你首先获得值。
<?php
if(isset($_POST) && !empty($_POST)){
echo "Successful post";
var_dump($_POST);
} else die("Error: post not successful");
?>
<?php
$servername = "localhost";
$username="root";
$password="";
$dbName="escape_room";
$user_username = $_POST['Input_user'];
$user_password = $_POST['Input_pass'];
$conn = new mysqli($servername, $username, $password, $dbName);
if(!$conn){
die("Cound not Connect: " . mysqli_connect_error());
}
$sql = "SELECT pass FROM escape_room WHERE user = '".$user_username."' ";
$result = mysqli_query($conn, $sql);
//Get the result and confirm login
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
if($row['pass'] == $user_password){
echo "login success";
echo $row['pass'];
}else{
echo "password incorrect";
echo "password is =". $row['pass'];
}
}
}else{
echo "user not found";
echo "password is =". $row['pass'];}
我在 Unity C 脚本中创建了一个 WWWForm,并试图通过服务器 XAMPP 上的 运行 Login.php 获取 Mysqlitedatabase 的值。但是,我无法确认 ID 是否匹配。只有用 html 语言编写的代码才会出现在控制台中。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Object not found!</title>
...
我该怎么办? 感谢您阅读。
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class gamemanager : MonoBehaviour {
[Header("LoginPanel")]
public InputField ID;
public InputField PW;
[Header("CreateAccountPanel")]
public InputField New_ID;
public InputField New_PW;
public string LoginUrI;
// Use this for initialization
void Start () {
LoginUrI = "localhost/escape_room/Login.php";
}
public void LoginBtn()
{
StartCoroutine(LoginToDB(ID, PW));
}
IEnumerator LoginToDB(InputField username, InputField password)
{
Debug.Log(username.text);
Debug.Log(password.text);
WWWForm form = new WWWForm();
form.AddField("Input_user", username.text);
form.AddField("Input_pass", password.text);
WWW webRequest = new WWW(LoginUrI, form);
yield return webRequest;
Debug.Log(webRequest.text);
}
public void CreateAccountBtn()
{
}
}
+添加) [ 这是我在 Unity C 脚本中实现的。我有一个 InputField,我使用 AddField 方法接受该 inputField 并将其传递给 php。此外,我创建了一个 WWW 对象以允许通过服务器访问 Login.php。最后,我通过 Debug.Log 进行了测试,以查看登录结果的外观。 ]
- 确认您在 xampp 上的路径,以确保这是您的目录路径 C:\xampp\htdocs\escape_room\Login.php
- 您实际上需要先从 unity post 表单,请参阅下面的代码
- 在没有 sql 的情况下单独调试 php 以确保您获得正确的数据。见代码。
WWW形式
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.Networking;
public class gamemanager : MonoBehaviour {
[Header("LoginPanel")]
public InputField ID;
public InputField PW;
[Header("CreateAccountPanel")]
public InputField New_ID;
public InputField New_PW;
string LoginUrI = "http://localhost/escape_room/login.php";
// Use this for initialization
void Start () {
}
public void LoginBtn()
{
StartCoroutine(LoginToDB(ID, PW));
}
IEnumerator LoginToDB(InputField username, InputField password)
{
Debug.Log(username.text);
Debug.Log(password.text);
WWWForm form = new WWWForm();
form.AddField("Input_user", username.text);
form.AddField("Input_pass", password.text);
// Make Post Request
using (var w = UnityWebRequest.Post(LoginUrI, form))
{
yield return w.SendWebRequest();
if (w.isNetworkError || w.isHttpError) {
Debug.Log(w.error);
}
else {
Debug.Log(w.downloadHandler.text);
}
}
}
public void CreateAccountBtn()
{
}
}
测试 php 代码,用这个替换你的 Login.php 以确保你首先获得值。
<?php
if(isset($_POST) && !empty($_POST)){
echo "Successful post";
var_dump($_POST);
} else die("Error: post not successful");
?>