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.