PHP代码访问MySQL数据库不回显

标签 php mysql heroku mysqli

很抱歉问我一个我认为相当简单的问题。 好的,所以我正在尝试关注 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/

相关文章:

php - 带有 html 页面的动态数据库驱动网站

python - 重定向所有对 www 的请求。到根域

node.js - 在 Node.js 中使用 Heroku 调度程序

heroku - 无法从 GitHub 帐户断开 Heroku 应用程序

php - Python 和 PHP 的异步日志记录策略

php - 使用两步身份验证/验证登录 Zend Gdata

php - 在 Symfony 3 Controller 中获取请求和 session

php mysql 在 INSERT 语句中转义字符

mysql - 在自动递增的 6-8 模式 mysql 中生成一个唯一的数字

mysql - 打开 MS SQL TCP 端口(安全)