Using Mysqli, error query

Asked

Viewed 404 times

-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’m making the following mistake:

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

From what I’ve seen it’s not much different from examples that other sites, but what really can it be? Or what’s the best way to do it?

  • you are using PDO or mysqli ?

  • 1

    I don’t understand, you quote PDO in the question but uses MySQLi throughout the code. The error message seems to me self-explanatory, the variable $consulta is an instance of the class mysqli_result and it does not have a method called fetch. Better organize your question so we can help you.

2 answers

3


Just change the line

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

for

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

1

To use a connection to PDO, call him and not the mysqli

in place of :

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

swap for:

$conn = new PDO('mysql:host=localhost;dbname=base', 'usuario', 'senha');
  • I get it, but in this case mysqli I’m using because I’m inside the zendframework and I’m kind of doing a 'gambiarra' myself. =(

Browser other questions tagged

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