C# 从PHP查询数据并在unity C中创建滚动列表#

C# 从PHP查询数据并在unity C中创建滚动列表#,c#,php,unity3d,C#,Php,Unity3d,我在Unity中有一个InputField,在编辑结束时,我将其设置为调用函数PopulateList(),以显示所有匹配查询命令的结果。如果我只得到一个结果,我的程序运行得很好,但是如果我得到两个或更多的结果,不管查询结果有多大,程序都会循环添加查询的第二个结果,我不知道为什么或者如何修复它。请帮我找到路 **我为我糟糕的英语写作技巧道歉 这是我的Unity C#代码: 公共类CreateSearchResult:MonoBehavior{ 公共文本搜索输入; 公开游戏对象搜索结果; 公共事务

我在Unity中有一个InputField,在编辑结束时,我将其设置为调用函数PopulateList(),以显示所有匹配查询命令的结果。如果我只得到一个结果,我的程序运行得很好,但是如果我得到两个或更多的结果,不管查询结果有多大,程序都会循环添加查询的第二个结果,我不知道为什么或者如何修复它。请帮我找到路

**我为我糟糕的英语写作技巧道歉

这是我的Unity C#代码:

公共类CreateSearchResult:MonoBehavior{
公共文本搜索输入;
公开游戏对象搜索结果;
公共事务委员会;
公共清单项目清单;
公众形象;
公共字符串queryURL=”http://http://localhost/dmcs/getdata.php?keyword=";
公共字符串imgURL=”http://http://localhost/dmcs/people/";
公共无效公共列表()
{
start例程(GetResult());
}
IEnumerator GetResult()
{
//清除以前的搜索结果
foreach(在contentPanel中转换子级)
{
GameObject.Destroy(child.GameObject);
}
WWW result_get=newwww(queryURL+searchInput.text);
得到的收益结果;
if(result\u get.error!=null)
{
打印(“出现错误:+result\u get.error”);
}
其他的
{
string[]result=result_get.text.Split(“;”.ToCharArray());

对于(int i=0;i除了关于SQL注入的注释外,我建议您使用诸如或之类的通用格式格式化数据,这将使您的生活随着项目的增加或更改而变得更轻松,在本例中,我将使用JSON,因为它更容易理解

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using SimpleJSON;

public class CreateSearchResult : MonoBehaviour
{
    public string queryURL = "http://http://localhost/dmcs/getdata.php?keyword=";
    public string imgURL = "http://http://localhost/dmcs/people/";

    public void PopulateList()
    {
        StartCoroutine(GetResult());
    }

    IEnumerator GetResult()
    {
        string jsonData = "";

        WWW result_get = new WWW(queryURL + searchInput.text);
        yield return result_get;

        if (result_get.error != null)
        {
            print("There was an error : " + result_get.error);
        }
        else
        {
            JSONNode jsonNode = JSON.Parse(jsonData);
            int resultQty = int.Parse(jsonNode["content"]);

            for (int i = 0; i < resultQty; i++)
            {
                string name = jsonNode["keyword"][i]["name"];
                string building = jsonNode["keyword"][i]["building"];
                // And so on... 
            }
        }
    }

您对查询非常开放,应该使用而不是串联查询。特别是因为您根本没有逃避用户输入!谢谢您的建议。非常感谢。我解决了它。问题是for循环I=+5我改为I++并在代码中使用I*5可以修复它,或者使用上面的代码也可以修复它。
<?php
// Configuration
$hostname = 'localhost';
$username = 'root';
$password = '';
$database = 'dmcs';

try {
    $dbh = new PDO('mysql:host='. $hostname .';dbname='. $database, $username, $password);
} catch(PDOException $e) {
    echo '<h1>An error has occurred.</h1><pre>', $e->getMessage() ,'</pre>';
}
$keyword = $_GET['keyword'];
$sth = $dbh->query("SELECT * FROM people WHERE name LIKE '%$keyword%' OR building LIKE '%$keyword%' OR floor LIKE '%$keyword%' OR room LIKE '%$keyword%'");
$sth->setFetchMode(PDO::FETCH_ASSOC);

$result = $sth->fetchAll();

foreach($result as $r) {
    echo $r['people_id'], ";", $r['name'], ";", $r['building'], ";", $r['floor'], ";", $r['room'], ";";
}?>
<?php

// Configuration
$hostname = 'localhost';
$username = 'root';
$password = '';
$database = 'dmcs';

try {
    $dbh = new PDO('mysql:host=' . $hostname . ';dbname=' . $database, $username, $password);
} catch (PDOException $e) {
    echo '<h1>An error has occurred.</h1><pre>', $e->getMessage(), '</pre>';
}

$sth = $dbh->prepare("SELECT * FROM people WHERE name LIKE %:keyword% OR building LIKE %:keyword% OR floor LIKE %:keyword% OR room LIKE %:keyword%");
$sth->execute([':keyword' => $_GET['keyword']]);
$sth->setFetchMode(PDO::FETCH_ASSOC);
$json['keyword'] = $sth->fetchAll();
$json['content'] = count($json['keyword']);

echo json_encode($json);
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using SimpleJSON;

public class CreateSearchResult : MonoBehaviour
{
    public string queryURL = "http://http://localhost/dmcs/getdata.php?keyword=";
    public string imgURL = "http://http://localhost/dmcs/people/";

    public void PopulateList()
    {
        StartCoroutine(GetResult());
    }

    IEnumerator GetResult()
    {
        string jsonData = "";

        WWW result_get = new WWW(queryURL + searchInput.text);
        yield return result_get;

        if (result_get.error != null)
        {
            print("There was an error : " + result_get.error);
        }
        else
        {
            JSONNode jsonNode = JSON.Parse(jsonData);
            int resultQty = int.Parse(jsonNode["content"]);

            for (int i = 0; i < resultQty; i++)
            {
                string name = jsonNode["keyword"][i]["name"];
                string building = jsonNode["keyword"][i]["building"];
                // And so on... 
            }
        }
    }