How to write this MySQL code correctly?

3

I'm trying to adapt to mysqli_ since mysql_ can be at any time discontinued now that new concepts like mysqli_ and pdo have appeared. I am trying to write this script that pulls the information from the database but it is not working. What could 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 reference, I ended up using PDO as indicated for security 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>
    
asked by anonymous 15.08.2014 / 16:09

2 answers

3

Change% from% to% with%. But I recommend using PDO because of the greater security it will acquire.

    
15.08.2014 / 16:14
2

Without the error it is difficult to help, but it seems that you are using the wrong function to go through the result.

Try changing $resultado->fetch_array() to $resultado->fetch_assoc()

Documentation: link link

    
15.08.2014 / 16:17