MySQLi multiple prepared statements using previous fetched $variable

112 views Asked by At
$stmt = $mysqli->prepare("SELECT id,name,master,level,exp FROM player.guild ORDER BY exp DESC");
$stmt->execute();
$stmt->bind_result($id, $name, $master, $level, $exp);
$stmt->fetch();
$guildnum = $stmt->num_rows;
$stmt->store_result();
$stmt->close();

$stmt2 = $mysqli->prepare("SELECT id,login FROM account.account WHERE id=?");
$stmt2->bind_param("i", $master);
$stmt2->execute();
$stmt2->bind_result($boss_id, $boss_name);
$stmt2->store_result();
$stmt2->close();

$stmt3 = $mysqli->prepare("SELECT empire FROM player.player_index WHERE id=?");
$stmt3->bind_param("i", $boss_id);
$stmt3->execute();
$stmt3->bind_result($empire);
$stmt3->store_result();
$stmt3->close();      

$stm2 and $stm3 are not returning the result..

//EXPLAINING THE CODE

$stm fetch some details and store them into $variables

$stm2 uses the variable $master to fetch "id" and "login" and store them into other variables

$stm3 uses $boss_id (stored by $stm2) to fetch and set the variable $empire

//EXPLAINING THE CODE

Can anyone help me please?

Regards.

1

There are 1 answers

1
Gerardo Charles Rojas Vega On BEST ANSWER

So if all is correct, just need to fetch the values (bind_result)

    $stmt = $mysqli->prepare("SELECT id,name,master,level,exp FROM player.guild ORDER BY exp DESC");
    $stmt->execute();
    $stmt->bind_result($id, $name, $master, $level, $exp);
    $stmt->fetch(); //fetch values
    $guildnum = $stmt->num_rows;
    $stmt->store_result();
    $stmt->close();

    $stmt2 = $mysqli->prepare("SELECT id,login FROM account.account WHERE id=?");
    $stmt2->bind_param("i", $master);
    $stmt2->execute();
    $stmt2->bind_result($boss_id, $boss_name);
    $stmt2->fetch(); //fetch values
    $stmt2->close();

    $stmt3 = $mysqli->prepare("SELECT empire FROM player.player_index WHERE id=?");
    $stmt3->bind_param("i", $boss_id);
    $stmt3->execute();
    $stmt3->bind_result($empire);
    $stmt3->fetch(); //fetch values
    $stmt3->close();