1

我正在运行一个使用 PHP 从 Web 服务器检索数据的移动应用程序。

当我在 sql 查询中运行 SQL 语句时,它会返回正确的数据。

当我运行 PHP 时,它为 gameArray 中位置 0 的项目返回空值,数组中的所有其他项目都显示正确的值

代码如下

$userResponse = array(
            'login' => $userName,
            'nickname' => $userNickName); 

$stmt = $conn->gamedb->prepare("SELECT player1, player2, currentturn, question1, question2, question3, question4, question5, question6, question7, question8, question9 FROM Game WHERE Player1 = ? OR Player2 = ?");
$stmt->bind_param('ss', $userName, $userName);
$stmt->execute();
$result = $stmt->store_result();

//create games array to be filled when stepping through the games
$gameResponse = array();

if($conn->gamedb->affected_rows > 0)
{
    while ($stmt->fetch())
    {
        $stmt->bind_result($player1, $player2, $currentTurn,
                           $question1, $question2, $question3,
                           $question4, $question5, $question6,
                           $question7, $question8, $question9);

        $gameArray = array($player1, $player2, $currentTurn,
                           $question1, $question2, $question3,
                           $question4, $question5, $question6,
                           $question7, $question8, $question9);


        //stores the game data into an array
        $gameResponse[] = $gameArray;
    }

    //close stmt
    $stmt->close();
}

$response = array(
            $userResponse,
            $gameResponse
            );

echo json_encode($response);

任何帮助都是极好的!

4

2 回答 2

1

我可以建议你尝试一些更简单的东西吗?

while($row = $result->fetch_assoc()) {
    $gameResponse[] = $row;
}

这样,您就不必经历分配变量等的所有过程。PHP 将为您做到这一点。

于 2013-10-14T03:27:19.200 回答
0

阅读手册页mysqli_stmt::fetch,它清楚地说明了

请注意,在调用mysqli_stmt_fetch()之前,所有列都必须由应用程序绑定。

试试这个...

$gameResponse = array();

$stmt->bind_result($player1, $player2, $currentTurn,
                   $question1, $question2, $question3,
                   $question4, $question5, $question6,
                   $question7, $question8, $question9);

while ($stmt->fetch()) {
    $gameResponse[] = array($player1, $player2, $currentTurn,
                            $question1, $question2, $question3,
                            $question4, $question5, $question6,
                            $question7, $question8, $question9);
}
于 2013-10-14T03:27:42.433 回答