如何将我的Unity项目连接到ec2实例上的mysql服务器

我正在尝试从我的MySQL服务器获取数据到我的Unity项目。 我试图得到这样的数据:</ p>

  public string awsurl =“ec2-174-129-82-141.compute-1.amazonaws.com/connect_to_server.php”  ; 
IEnumerator GetScores()
{
print(“得分开始”);
WWW aws_get = new WWW(awsurl);
yield return aws_get;
print(“getscore here”);
if (aws_get.error!= null)
{
print(“获取aws时出错:”+ aws_get.error);
}
else
{
print(aws_get.text); //这是一个GUIText,它将显示游戏中的分数。
}
}
</ code> </ pre>

我的服务器上有一个PHP脚本来处理交互,因为 我在使用MySqlConnection库进行统一项目时遇到问题。

这是我的php脚本:</ p>

  $ address =“localhost”
$ dbusername =“root”;
$ dbpassword =“root”;
$ db_name = “watshoes”;
$ db_conn = new mysqli($ address,$ dbusername,$ dbpassword,$ db_name);
if(isset($ _ POST ['username']))$ username = $ _POST ['username'] ;
if(isset($ _ POST ['user_id']))$ user_id = $ _POST ['user_id'];
if(isset($ password))$ password = $ _ POST ['password'];

$ stmt = $ db_conn-&gt; prepare(“SELECT image FROM ImageText”);
//“s”表示数据库需要字符串
$ stmt-&gt; bind_param(“s”,$ user_id);

if($ stmt-&gt; execute())
{
/ 绑定结果变量 /
$ stmt-&gt; bind_result($ image);
/ * fetch value * /
$ stmt - &gt; fetch();
echo $ image;
}
其他
{
echo“查询失败”;
}
$ stmt-&gt; close();
$ db_conn-&gt ; close();
</ code> </ pre>

但每次运行代码时都会收到此错误:获取aws时出错:无法连接到ec2- 174-129-82- 141.compute-1.amazonaws.com端口80:超时</ code> </ p>

感谢您查看代码并告诉我是否还有其他任何可以帮助的方法 。</ p>
</ div>

展开原文

原文

I am trying to get data from my MySQL server to my Unity project. I tried to get the data like so:

public string awsurl = "ec2-174-129-82-141.compute-1.amazonaws.com/connect_to_server.php";
IEnumerator GetScores()
{
        print("get scores start");
        WWW aws_get = new WWW(awsurl);
        yield return aws_get;
        print("getscore here");
        if (aws_get.error != null)
        {
            print("There was an error getting aws: " + aws_get.error);
        }
        else
        {
            print(aws_get.text); // this is a GUIText that will display the scores in game.
        } 
}

I have a php script on my server to handle the interaction because I am having trouble using the MySqlConnection library for my unity project. Here is my php script:

    $address = "localhost"
$dbusername = "root";
$dbpassword = "root";
$db_name = "watshoes";
$db_conn = new mysqli($address, $dbusername, $dbpassword, $db_name);
    if(isset($_POST['username'])) $username = $_POST['username'];
    if(isset($_POST['user_id'])) $user_id = $_POST['user_id'];
    if(isset($password)) $password =$_POST['password'];

    $stmt = $db_conn->prepare("SELECT image FROM ImageText");
    // "s" means the database expects a string
    $stmt->bind_param("s", $user_id);
    if($stmt->execute())
    {
        /* bind result variables */
        $stmt->bind_result($image);
        /* fetch value */
        $stmt->fetch();
        echo $image;
    }
    else
    {
        echo "query failed";
    }
    $stmt->close();
    $db_conn->close();

But every time I run the code I get this error:There was an error getting aws: Failed to connect to ec2-174-129-82-141.compute-1.amazonaws.com port 80: Timed out

Thanks for looking at the code and please let me know if there is anything else I can do to help.

1个回答

You have to add a rule to open the port 80 using security groups (Inbound tab)

How your rule should look:

How should look your rule

By default the port 80 is close in EC2 AWS instances.

And the URL is malformed in Unity Add http:// to your server URL In editor will work ok without http:// but it will fail in android.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐