Using MySQLi, query with error

-3

I have the following code:

$escolha = $_POST['unidade'];

if($escolha == 'ut')
{
    $conn = new mysqli($host1, $user, $pass, $bd);
        if (mysqli_connect_errno()) {
            die(mysqli_connect_error());
            exit();
        }else{
            $consulta = $conn->query("SELECT nm_usu FROM usuarios;");
            while ($resultado = $consulta->fetch()) {
                echo "Nome: {$resultado['nm_usu']}<br/>";
            }
        }
}

And I have the following error:

Fatal error: Call to undefined method mysqli_result :: fetch () in

From what I've seen is not much different from examples than other sites, but what can it really be? Or what is the best way to do this?

    
asked by anonymous 29.04.2014 / 15:34

2 answers

3

Just change the line

while ($resultado = $consulta->fetch()) {

by

while ($resultado = $consulta->fetch_array()) {
    
29.04.2014 / 15:51
1

To use a connection with PDO , call it instead of mysqli

instead of:

$conn = new mysqli($host1, $user, $pass, $bd);

Switch By:

$conn = new PDO('mysql:host=localhost;dbname=base', 'usuario', 'senha');
    
29.04.2014 / 15:51