用准备好的语句获取多行 [英] Getting multiple rows with prepared statement

查看:56
本文介绍了用准备好的语句获取多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对准备好的语句还很陌生,不确定我是否做对了.

I'm quite new to prepared statements and am not sure I am doing this right.

这是我尝试的方法:

$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();"这会导致错误,但不确定原因.请帮忙.

I get an "ServerError" when trying to run this: It is the "$res = $stmt->get_result();" that makes the error, but not sure why. Please help.

预先感谢:-)

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

推荐答案

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

Depending on your PHP/MySQL setup you may not be able to use get_result().

解决此问题的方法是绑定结果.

The way to get around this is to bind the results.

例如:

$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.*/
}

有关更多信息,请单击此处

这篇关于用准备好的语句获取多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆