0

Before anything else this is my PHP Code

<?php
    $conn = mysqli_connect("localhost", "root", "", "mydb");
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    $promo1 = "";
    $promo2 = ""; 
    $promo3 = "";

    $sql = "SELECT prmo_itemDescription FROM promos";
    $result = mysqli_query($conn, $sql);

    $row = mysqli_fetch_array($result);
          $promo1 = $row[0];
          $promo2 = $row[1];
          $promo3 = $row[2];
    mysqli_free_result($result);
    mysqli_close($conn);
    ?>

and this is where I want the description to be echoed

      <div class="col-md-3">
          <p><?php echo $promo1?></p>
            <button type="button" class="btn btn-success" data-toggle="modal" data-target="#details-1">Buy Now!</button>
        </div>

        <div class="col-md-3">
          <p><?php echo $promo2?></p>
            <button type="button" class="btn btn-success" data-toggle="modal" data-target="#details-2">Buy Now!</button>
        </div>

        <div class="col-md-3">
          <p><?php echo $promo3?></p>
            <button type="button" class="btn btn-success" data-toggle="modal" data-target="#details-3">Buy Now!</button>
        </div>

and this is my database in phpmyadmin My problem is I managed to display the $promo1 but the other two(promo2 and promo3) doesnt seem to display, it looks like it cannot find the other two rows. There's an error like unidentified offset. am I missing something?

4
  • php.net/manual/en/mysqli-result.fetch-array.php - mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both Commented Oct 1, 2018 at 10:47
  • You've selected only prmo_itemDescription column in your SQL query, that's why you can't display the other ones. Commented Oct 1, 2018 at 10:49
  • You are only reading one single row of the result set, and pretend like that contained all the descriptions in multiple columns - which it doesn’t. You need to loop over the result, to output the data from each single row in the result set. Commented Oct 1, 2018 at 10:51
  • youve fetched one row, and assigned columns not rows to each var Commented Oct 1, 2018 at 11:13

1 Answer 1

1

It looks like you are trying to iterate over your results. You could do it with a loop, something like this

<?php
    //Create Connection
    $conn = new mysqli("localhost", "root", "", "mydb");
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "SELECT prmo_itemDescription as pname FROM promos";
    if ($promos = $conn->query($sql)) {
        $index = 1;
        while ($promo = $promos->fetch_assoc()) {
?>
    <div class="col-md-3">
        <p><?php echo $promo['pname']; ?></p>
        <button type="button" class="btn btn-success" data-toggle="modal" data-target="#details-<?php echo $index; ?>">Buy Now!</button>
    </div>
<?php
        $index += 1;
        } //END WHILE

        $promos->free();

    } //END IF

    $conn->close();
?>

Instead of index you could use promo id and use it on your html.

Sign up to request clarification or add additional context in comments.

2 Comments

at first I didn't understand what you did then I read it carefully. Not only you accurately solved my problem you also shortened the code, appreciate it.
The idea is to use the power of programming to don't repeat code, today you may need just 3 promos but tomorrow you could have 5 or much more, with a loop you don't have to worry about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.