很抱歉问我一个我认为相当简单的问题。 好的,所以我正在尝试关注 https://www.skysilk.com/blog/2018/how-to-connect-an-android-app-to-a-mysql-database/ ,一个关于如何按照它所说的去做的教程。 然而,我的 PHP 代码,当我按照他们说的方式测试它时,通过在浏览器中加载“herokuserverbeingused.com/phpcode.php”,而不是得到它说我应该得到的回显,我什么也没得到。 这是我的代码,它实际上只是对教程中的代码稍加修改
<?php
//borrowing from don't forget to credit https://www.skysilk.com/blog/2018/how-to-connect-an-android-app-to-a-mysql-database/
$con=mysqli_connect("a heroku server.net","nope","sorry","very secret");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM players";
// Confirm there are results
if ($result = mysqli_query($con, $sql))
{
// We have results, create an array to hold the results
// and an array to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each result
while($row = $result->fetch_object())
{
// Add each result into the results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?>
我使用 Heroku 来托管一切,因为我的教授说这是一个好主意,这就是导致问题的原因吗?
最佳答案
原来问题是 $resultArray 中的内部行是 stdClass 对象而不是数组,解决方案是替换 $temparray = $row
与 $tempArray = json_decode(json_encode($row), True);
感谢 Dan 帮助我找出问题所在以及回答 Convert stdClass object to array in PHP 的人向我展示最简单的解决方案
关于PHP代码访问MySQL数据库不回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51583694/