访问mysql数据库的php代码没有回音

67up9zun  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(492)

对不起,我想问一个相当简单的问题。好吧,我试着跟着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来主持所有的事情,因为我的教授说这是个好主意,这就是问题的根源吗?

m4pnthwp

m4pnthwp1#

原来问题是$resultarray中的内部行是stdclass对象而不是数组,解决方案是替换 $temparray = $row$tempArray = json_decode(json_encode($row), True); 感谢dan帮我找出了问题所在,以及那个在php中回答convert stdclass object to array的人向我展示了最简单的解决方案

相关问题