5

我对准备好的陈述很陌生,我不确定我做对了。

这是我尝试的:

$currgame = 310791;

$sql = "SELECT fk_player_id, player_tiles, player_draws, player_turn, player_passes, swapped FROM ".$prefix."_gameplayer WHERE fk_game_id = ?";
$stmt = $mysqli->stmt_init();

$data = array();
if($stmt->prepare($sql)){
    $stmt->bind_param('i', $currgame);
    $stmt->execute();

    $fk_player_id = null; $player_tiles = null; $player_draws = null; $player_turn = null; $player_passes = null; $swapped = null;
    $stmt->bind_result($fk_player_id, $player_tiles, $player_draws, $player_turn, $player_passes, $swapped);

    $res = $stmt->get_result();

    while ($row = $res->fetch_assoc()){
        $data[] = $row;
    }
    $stmt->close(); 
}

// to display own games
foreach ($data as $row) {
    if ($row['fk_player_id'] == $playerid) {

        $udraws = $row['player_draws']+1; 
        $upass = $row['player_passes'];
        $uswaps = $row['swapped'];

        echo 'uDraws: '.$udraws.'<br>';
        echo 'uPass: '.$upass.'<br>';
        echo 'uSwaps: '.$uswaps.'<br><br>';
    }
}
// to display other games
foreach ($data as $row) {
    if ($row['fk_player_id'] != $playerid) {

        $opponent = $row['fk_player_id'];
        $oppTiles = $row['player_tiles'];

        $odraws = $row['player_draws']+1;
        $opass = $row['player_passes'];
        $oswaps = $row['swapped'];

        echo 'oID: '.$opponent.'<br>';
        echo 'oTiles: '.$oppTiles.'<br>';

        echo 'oDraws: '.$odraws.'<br>';
        echo 'oPass: '.$opass.'<br>';
        echo 'oSwaps: '.$oswaps.'<br><br>';

    }
}

尝试运行此程序时出现“ServerError”:它是“$res = $stmt->get_result();” 这会导致错误,但不知道为什么。请帮忙。

提前致谢 :-)

#### 编辑
$sql = "SELECT fk_player_id, player_tiles, player_draws, player_turn, player_passes, swapped FROM ".$prefix."_gameplayer WHERE fk_game_id = ?";
$stmt = $mysqli->stmt_init();

$data = array();
if($stmt->prepare($sql)){
    $stmt->bind_param('i', $currgame);
    $stmt->execute();

    $fk_player_id = null; $player_tiles = null; $player_draws = null; $player_turn = null; $player_passes = null; $swapped = null;
    $stmt->bind_result($fk_player_id, $player_tiles, $player_draws, $player_turn, $player_passes, $swapped);

    while ($row = $stmt->fetch()){
        $data[] = $row;
    }
    $stmt->close(); 
}

echo '<pre>';
print_r($data);
echo '</pre>';
4

3 回答 3

14

根据您的 PHP/MySQL 设置,您可能无法使用 get_result()。

解决这个问题的方法是绑定结果。

例如:

$stmt->execute();

$fk_player_id = null; $player_tiles = null; $player_draws = null; $player_turn = null; $player_passes = null; $swapped = null;

$stmt->bind_result($fk_player_id, $player_tiles, $player_draws, $player_turn, $player_passes, $swapped);

while ($stmt->fetch()) { // For each row
    /* You can then use the variables declared above, which will have the 
    new values from the query every time $stmt->execute() is ran.*/
}

欲了解更多信息,请点击此处

于 2013-02-03T10:47:15.270 回答
-1

由于我在您的代码中没有看到它,因此请确保mysqli在尝试查询对象之前实例化该对象:

$mysqli = new mysqli("127.0.0.1", "user", "password", "mydb"); 
if($mysqli->connect_error){
    die("$mysqli->connect_errno: $mysqli->connect_error");
}

此外,aServerError肯定会出现在您的日志中,并为您指明正确的方向。

于 2013-02-03T10:42:32.290 回答
-1
while (mysqli_stmt_fetch($stmt)) {
        printf ("%s (%s)\n", $name, $code);
    }

这可能会帮助您:

http://php.net/manual/en/mysqli-stmt.fetch.php

于 2013-07-03T18:22:58.693 回答