我的服务器上有一个在线数据库,其中每个用户名都分配有一个主键 ID。我想在 PHP 的帮助下从表中获取这个 id。代码如下: `
<?php
$servername = "uk9.siteground.eu";
$username = "****";
$password = "****";
$dbname = "****";
$tablename = "Account";
$user = $_REQUEST["LoggedInUser"];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id
FROM $tablename
WHERE 'username' LIKE '$user'";
$result = $conn->query($sql)->fetch_object()->id;
echo $result;
mysqli_close($conn);
?>
`
当我通过浏览器使用 PHP 脚本时,它会工作,它会打印正确的 ID 号。但是,当我通过 Unity 调用此 PHP 时: `
public string username;
public string userID;
private void Start()
{
username = "example";
StartCoroutine(GetUID());
}
IEnumerator GetUID()
{
string url = "****";
WWWForm form = new WWWForm();
form.AddField("LoggedInUser", username);
WWW wwwResponse = new WWW(url, form);
Debug.Log("Connecting to PHP");
yield return wwwResponse;
if (wwwResponse.error != null)
Debug.Log(wwwResponse.error);
else
Debug.Log("No Error" + wwwResponse.text);
userID = wwwResponse.text;
Debug.Log(userID);
}
` 代码到达“连接到 PHP”点,但此后不会在控制台中打印任何内容。
谁能帮我解决这个问题吗?谢谢
最佳答案
移动
yield return wwwResponse;
到代码底部。
示例:
IEnumerator GetUID() {
string url = "****";
WWWForm form = new WWWForm();
form.AddField("LoggedInUser", username);
WWW wwwResponse = new WWW(url, form);
//wait till backend responses your request
while(!wwwResponse.isDone) {
yield return null; //skip frame
}
// keep going when you got your response
if(wwwResponse.error != null) {
Debug.Log(wwwResponse.error);
} else {
Debug.Log("No Error" + wwwResponse.text);
userID = wwwResponse.text;
Debug.Log(userID);
}
yield return wwwResponse;
}
关于php - 无法使用 PHP 从 MySQL 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48824317/