1

I am running a making a mobile app that uses PHP to retrieve data from a web server.

When I run the SQL statement in the sql query it returns the correct data.

when I run the PHP it returns null values for the item at location 0 in the gameArray, all other items in the array show the right values

the code is as follows

$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);

any help would be awesome!

1
  • I wouldn't bother with checking affected_rows Commented Oct 14, 2013 at 3:26

2 Answers 2

1

Could I suggest you try something a bit simpler?

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

This way you don't have to go through all the gyrations of assigning variables and such. PHP will do that for you.

Sign up to request clarification or add additional context in comments.

Comments

0

Reading the manual page for mysqli_stmt::fetch, it clearly states

Note that all columns must be bound by the application before calling mysqli_stmt_fetch().

Try this instead...

$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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.