0

I am able to grab the current poll_id fine and get it to echo out the correct value.

I'm also trying to echo the value from the next row in the table at the moment just the number but it's so I can insert a link to the next poll.

I keep getting the Next Poll Not Available, although I know that there is a "next" poll.

What am I doing wrong?

// Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT poll_id, poll_question FROM poll_main LIMIT 5, 1";
        $nxt = "SELECT poll_id FROM poll_main WHERE ID > ? LIMIT 1";
        $result = $conn->query($sql);
        $nxtres = $conn->query($nxt);

        if ($result->num_rows > 0) {

            while($row = $result->fetch_assoc()) {
                echo "<input type='hidden' id='poll_id' value='" . $row["poll_id"]. "'/>";
            }
        } else {
            echo "Current Poll Not Available";
        }

        if ($nxtres->num_rows > 0) {

            while($row = $nextres->fetch_assoc()) {
                echo $row["poll_id"];
            }
        } else {
            echo "Next Poll Not Available";
        }

        $conn->close();
Dharman
  • 30,962
  • 25
  • 85
  • 135
Mike
  • 87
  • 1
  • 2
  • 10
  • 2
    You have a `?` in your query, so you need to prepare, bind and execute this rather than just run qeury. Also the query probably would need an order by to ensure you get the next id and not just some random one. – Nigel Ren Apr 04 '20 at 11:38
  • Note that LIMIT without ORDER BY is fairly meaningless (admittedly, with the exception of `LIMIT 0,1`) – Strawberry Apr 04 '20 at 11:39
  • You also have a typo - `$nxtres` in some parts, and then `$nextres` when you try to fetch the results. – droopsnoot Apr 04 '20 at 11:57
  • Does this answer your question? [How to include a PHP variable inside a MySQL statement](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) – Dharman Apr 04 '20 at 12:00
  • @NigelRen thanks Nigel. Would it be possible to do this without the ? or is this the correct way to do it, because I'm a bit confused. – Mike Apr 04 '20 at 13:10
  • @Strawberry thanks, will bear ORDER BY in mind, I've added it to my code. – Mike Apr 04 '20 at 13:14
  • @droopsnoot good catch, have updated. thanks. – Mike Apr 04 '20 at 13:15

0 Answers0