Mysqli prepared statements pomoc

Zasto unutar while petlje kad koristim mysqli bez prepared statements mogu da izvrsim jos jedan query, a kad koristim prep. statements ne mogu?

Konkretno evo o cemu se radi:

    $sql = "SELECT * FROM topics ORDER BY topic_id DESC";
    $result = $mysqli->query($sql);

<?php   while($row = $result->fetch_assoc()) {
  $topic_id = $row['topic_id'];

  ?>

 <div class="brpostova col-md-2">Postova: 
         <?php
         $sql2 = "SELECT * FROM postovi WHERE post_topic = $topic_id ";
         $rezultat = $mysqli->query($sql2);
         echo $rezultat->num_rows;
?>
          
</div>
        <div class="autor col-md-2">Posted by <a href="#"><?php echo $row['topic_autor']; ?></a></div>

 <?php 
}
     $result->free
?>

Znaci ovo mi radi okej, a kad idem sa prepared statements nece:

$stmt = $mysqli->prepare("SELECT * FROM topics ORDER BY topic_id DESC");
$stmt->execute();
$stmt->bind_result($id, $sadrzaj, $naslov, $datum, $kategorija, $autor, $sticky, $closed);

<?php while ($stmt->fetch())    {
      $topic_id = $id;
 
      ?>
    
     <div class="brpostova col-md-2">Postova: 
             <?php
             $post = $mysqli->prepare("SELECT * FROM postovi WHERE post_topic = ? ");
                  $post->bind_param('i', $topic_id);
                  $post->execute();
                  $post->store_result();
                  $numrows = $post->num_rows;
                  $post->close();
                echo $numrows
    ?>
              
    </div>
            <div class="autor col-md-2">Posted by <a href="#"><?php echo $autor; ?></a></div>
    
     <?php 
    }
         $stmt->free_result();
         $stmt->close();
    ?>

Ima li neko ideju sto nece?

$stmt = $mysqli->prepare("SELECT * FROM topics ORDER BY topic_id DESC");
$stmt->execute();
$stmt->store_result(); //probaj dodati store_result
$stmt->bind_result($id, $sadrzaj, $naslov, $datum, $kategorija, $autor, $sticky, $closed);

Nece , store_result() treba kad se poziva $stmt->num_rows…

store_result() se koristi i za iteriranje po rezultatima, a to ti upravo želiš napraviti.

Objašnjenje ti je ovdje: http://www.ioncannon.net/programming/889/php-mysqli-and-multiple-prepared-statements/ nadam se da će ti pomoć


Copyright © 2020 WM Forum - AboutContact - Sponsored by: Mydataknox & Profit Monkey