通过XAMPP使用Unity C#访问Sqlitedatabase时,我得到奇怪的结果

时间:2018-11-21 04:43:06

标签: php sqlite unity3d login 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进行了测试,以查看登录结果看起来有多好。 ]

1 个答案:

答案 0 :(得分:0)

  1. 在xampp上确认您的路径,以确保这是您的目录路径C:\ xampp \ htdocs \ escape_room \ Login.php
  2. 您需要首先从统一实际发布表单,请参见下面的代码
  3. 不使用sql单独调试php,以确保获得正确的数据。参见代码。

WWWform

    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");
?>