mysqli_query () does not return false

0

Starting in PHP, I recently learned OO with PHP and had a problem converting my procedural code to OO. I have an "organ" table (columns: id, name_orgao) with only 1 record. I query through a DAO (I loved this):

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 database ...

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 me false, I believe it returns an empty mysqli_result or something. When I give a var_dump ($ result) it returns me this:

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

Comments:

  • the connection to the database is included in the header.php.
  • also in the header, the autoloading is being done classes.
  • I have no idea what's wrong because in procedural mode It worked perfectly.
asked by anonymous 25.09.2016 / 21:53

1 answer

0

The result obtained with the return of mysqli_query may be a mysqli_result or bool (false) . When return is false , you have a problem with SQL , syntax error for example , when return is mysqli_result the result was obtained even though it has no records, including whether the mysqli_result has a record with ->num_rows if it is greater than 0 has record in the table .

Your code would look 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 would summarize your code this 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();
}

This would be a base code, just missing the class DAO .

    
25.09.2016 / 23:01