0

Hi I have this code which works

    $sql = "SELECT * FROM rock_news ORDER BY id DESC LIMIT 3";
    $result = $dbCon->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_object()) {
            $id = $row->id;
            $heading = utf8_encode($row->heading);
            $subheading = utf8_encode($row->subheading);
            $description = utf8_encode($row->description);
            $created = $row->created;
            $author = utf8_encode($row->author);
            ?>
    <div class="news-wrapper flex">
        <div class="col10">
            <div class="id-wrapper">
                <p class="news-id"><?php echo $id?></p>
                <div class="arrow-left"></div>
            </div>
        </div>
        <div class="col90 news-info-wrapper flex-d-c">
            <div class="news-publisher-wrapper flex space-b">
                <h2 class="center tekst2"><?php echo $author?></h2>
                <h2 class="tekst2"><?php echo $created?></h2>
            </div>
            <p class="news-publisher-wrapper-text flex1"><?php echo $heading ?></p>
            <div class="news-more flex center-r">
                <a class="center" href="index.php?page=newsTest&id=<?php echo $id ?>">Læs mere</a>
            </div>
        </div>
    </div>

            <?php
        }
    }
    $dbCon->close();
    ?>

But I want to use prepared statements, but I cant figure out how to do it with SELECT. I have made it with INSERT but this is a bit different?

This is what I have so far, I am not sure what is going on from $meta = ?. I am not sure on how the while loop works here or which parameters I should put in $value, $value2 etc? Could anyone explain to me how this work? It's from this Stack Overflow thread. The answer is upvoted 44 times: SELECT * FROM in MySQLi

            $stmt = $mysqli->prepare('SELECT heading, subheading, description, created, author FROM rock_news WHERE field1 = ? AND field2 = ? AND field3 = ? AND field4 = ? AND field5 = ? AND field5 = ?');
            $stmt->bind_param('ssssss', $value, $value2, $value3, $value4, $value5, $value6);
            $stmt->execute();

            $meta = $stmt->result_metadata();

            while ($field = $meta->fetch_field()) {
              $parameters[] = &$row[$field->name];
            };

            call_user_func_array(array($stmt, 'bind_result'), $parameters);
            while ($stmt->fetch()) {
              foreach($row as $key => $val) {
                $x[$key] = $val;
              }
              $results[] = $x;
            };
Community
  • 1
  • 1

0 Answers0