How to write this Mysql code correctly?

Asked

Viewed 196 times

3

I am trying to adapt tomysqli_ sincemysql_ can be discontinued at any time now that new concepts likemysqli_ and Pdo have emerged. I am trying to write this script that pulls the database information but is not working. What may be wrong?

        <select id="rsvpQuem" name="rsvpQuem">
            <option value="">Selecione uma opção...</option>
            <?php
            $query  = "SELECT * FROM tbl_convidados";
            $result = $mysqli->query($query);

            while($row = $result->fetch_array()){ ?>
                <option value="<?=$row['id']?>"><?=$row['nome']?></option>  
            <?php } ?>
        </select>

Leaving a solution to my problem for future queries, I ended up using PDO as indicated for security reasons and ease. Below the code:

        <select id="rsvpQuem" name="rsvpQuem">
            <option value="">Selecione uma opção...</option>
            <?php
                $consulta = $db->prepare('SELECT * FROM tbl_convidados');
                $consulta->execute();
                while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
                    echo "<option value=".$linha['id'].">".$linha['nome']."</option>";
                }
            ?>
        </select>
  • Error appears?

  • No error appears.

  • In select appear several rows of empty options? tested the direct query in the database?

  • use this class is very good. read the README and see the most basic examples. https://github.com/offboard/Class-Query

2 answers

3


Alter $result = $mysqli->query($query); for $result = mysqli::query($query);. But I recommend the use of PDO, due to the greater security that will acquire.

  • my Dreamweaver is pointing out an error in the line referring to the code $result = $mysqli::query($query);

  • 1

    it’s without $. so $result = mysqli::query($query);

  • 1

    I ended up using PDO as recommended and it already worked. I thank everyone in the OS for the layout and help they all offer to beginners in programming.

2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.