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.
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.
– Carlos Gabriel