mysqli_query() does not return false

Asked

Viewed 623 times

2

Starting in PHP, I recently learned OO with PHP and had a little problem when converting my procedural code to OO. I have an "orgao" table (columns: id, org_name) with only 1 record. I query through a DAO (I love it):

class OrgaoDao{
private $conexao;

function __construct($conexao){
    $this->conexao = $conexao;
}

public function buscaOrgao($nome_orgao){
    $qry = "SELECT * FROM orgao WHERE nome_orgao = '{$nome_orgao}'";
    if($resultado=mysqli_query($this->conexao,$qry)){
        $orgao_buscado = mysqli_fetch_assoc($resultado);
        $orgao = new Orgao($orgao_buscado['nome_orgao']);
        $orgao->setId($orgao_buscado['id_orgao']);
        return $orgao;
    } else {
        return false;
    }
}
}

But when I query a record that does not exist in the bank...

require_once 'cabecalho.php';
$orgaoDao = new OrgaoDao($conexao);
if($orgao = $orgaoDao->buscaOrgao($_GET['campo_orgao'])){
    echo $orgao->getNome();
    echo " ==> Encontrado";
} else {
    echo '==>  nao encontrado';
}

...mysql_query() does not return false, I believe it returns an empty mysqli_result or something like that. When I give a var_dump($result) it returns it to me:

object(mysqli_result)#3 (5) {
  ["current_field"]=>
  int(0)
  ["field_count"]=>
  int(3)
  ["lengths"]=>
  NULL
  ["num_rows"]=>
  int(0)
  ["type"]=>
  int(0)
}

Remarks:

  • connection to the bank is included in the.php header.
  • also in the header, is being done autoloading (delight) of classes.
  • I have no idea what’s wrong because in procedural mode worked perfectly.

1 answer

1


The result obtained with the return of mysqli_query can be a mysqli_result or bool (false). When the return is false, has some problem with the SQL, syntax error for example, when the return and mysqli_result the result was obtained even if it has no record, inclusive informs if the mysqli_result has record with ->num_rows if it is larger than 0 has table entry.

Your code would have to be like this:

public function buscaOrgao($nome_orgao)
{
    $qry = "SELECT * FROM orgao WHERE nome_orgao = '{$nome_orgao}'";
    if($resultado = mysqli_query($this->conexao,$qry))
    {
        if ($resultado->num_rows > 0) 
        {
            $orgao_buscado = mysqli_fetch_assoc($resultado);
            $orgao = new Orgao($orgao_buscado['nome_orgao']);
            $orgao->setId($orgao_buscado['id_orgao']);
            return $orgao;
        }
    } 
    return false;
}

I’d summarize your code that way

Orgao Class

<?php

    class Orgao {
        private $id;
        private $nome_orgao;

        public function getId()
        {
            return $this->id;
        }
        public function setId($id)
        {
            $this->id = $id;
            return $this;
        }
        public function getNomeOrgao()
        {
            return $this->nome_orgao;
        }
        public function setNomeOrgao($nome_orgao)
        {
            $this->nome_orgao = $nome_orgao;
            return $this;
        }
    }

Code:

$conn = mysqli_connect('localhost','root','senha', 'test');

$result = mysqli_query($conn, 'SELECT * FROM orgao');

if ($result && $result->num_rows > 0)
{
    $orgao = mysqli_fetch_object($result, Orgao::class);
    var_dump($orgao);
    //echo $orgao->getId();
    //echo $orgao->getNomeOrgao();
}

That would be a base code, all that’s left to put in the class DAO.

  • 1

    Perfect! I got it right now, I thought the return would be false if it did not find record in the table. Thank you very much, solved a whole day of burned neurons.

Browser other questions tagged

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