Treats error to connect to PHP database[rectifying as the answer]

Asked

Viewed 33 times

0

(edited this post to show the error that appeared when I updated the code with the help of the first reply)

I am building a database connection object. It is showing the following error.

Warning: mysqli_query() expects Parameter 1 to be mysqli, null Given in C: xampp htdocs Painel_de_control classes bank.class.php on line 67

Fatal error: Uncaught Error: Call to Undefined Function mysqli_ernno() in C: xampp htdocs Painel_de_control classes bank.class.php:76 Stack trace: #0 C: xampp htdocs Painel_de_control classes bank.class.php(67): bank->treatErro('C: xampp htdocs...', 'executeSQL') #1 C: xampp htdocs Painel_de_control classes bank.class.php(61): bank->executeSQL('INSERT INTO cli...') #2 C: xampp htdocs Painel_de_controle index.php(18): banco->inserirDados(Object(clients)) #3 {main} thrown in C: xampp htdocs Painel_de_control classes bank.class.php on line 76

Line 67 is inside the public function executeSQL. Inside it has a function mysqli_query() that is calling the above function of connection to the database.

<?php
abstract class banco{

//PROPRIEDADES
public $servidor = "localhost:84";
public $usuario = "root";
public $senha = "";
public $nomeBanco = "ourlady";
public $conexao = NULL;
public $dataset = NULL;//resultados das pesquisas
public $linhasAfetadas = -1;

//METODOS
public function __construct(){
    $this->connecta();

}//fim construtor

public function __destruct(){
    if($this->conexao != NULL):
        mysqli_close($this->conexao);
    endif;
}//fim destrutor

public function connecta(){
    $con = $this->conexao = mysqli_connect($this->servidor,$this->usuario, $this->senha) or die($this->tratarErro(__FILE__,__FUNCTION__,mysqli_errno(),errorInfo(),True));
    mysqli_select_db($con,$this->nomeBanco) or die($this->tratarErro(__FILE__,__FUNCTION__,mysqli_errno(),errorInfo(),True));
    mysqli_set_charset($con,'utf8');


}// fim connecta

public function inserirDados($objeto){
    $sqlInserir = "INSERT INTO ".$objeto->tabela." (";
    //loop inseri valores dentro do comando sql para ser inserido no banoc de dados
    for($i=0; $i<count($objeto->valores); $i++):
        $sqlInserir .= key($objeto->valores);
        //validação para inserir a virgula no meio do loop
        if($i < (count($objeto->valores)-1)):
            $sqlInserir .= ", ";
        else:
            $sqlInserir .= ") ";
        endif;
        next($objeto->valores);
endfor;
reset($objeto->valores);
$sqlInserir .= "VALUES (";
for($i=0; $i<count($objeto->valores); $i++):
    // validação tenaria para puxar o segundo valor da array
    $sqlInserir .= is_numeric($objeto->valores[key($objeto->valores)]) ? 
        $objeto->valores[key($objeto->valores)] :
        "'".$objeto->valores[key($objeto->valores)]."'";
    //validação para inserir a virgula no meio do loop
    if($i < (count($objeto->valores)-1)):
        $sqlInserir .= ", ";
    else:
        $sqlInserir .= ") ";
    endif;
    next($objeto->valores);
endfor;
return $this->executeSQL($sqlInserir);
echo $sqlInserir;
}// fim inserir daddos

public function executeSQL($sqlexe=NULL){
    if($sqlexe!=NULL):
        $query = mysqli_query($this->connecta(),$sqlexe) or $this->tratarErro(__FILE__,__FUNCTION__);
    else:

    endif;
}// fim da execução sql

public function tratarErro($arquivo=NULL, $rotina=NULL, $numErro=NULL, $msgErro=NULL, $geraExcept=FALSE){
    if($arquivo==NULL) $arquivo="não informado";
    if($rotina==NULL) $rotina="não informado";
    if($numErro==NULL) $numErro=mysqli_ernno($this->conexao);
    if($msgErro==NULL) $msgErro=mysqli_error($this->conexao);
    $resultado = 'Ocorreu o seguinte erro:<br/>
                    <b>Arquivo</b>'.$arquivo.'<br/>
                    <b>Rotina</b>'.$rotina.'<br/>
                    <b>Erro</b>'.$numErro.'<br/>
                    <b>Mensagem</b>'.$msgErro;
    if($geraExcept==False):
        echo($resultado);
    else :
        die($resultado);

    endif;

 }// fim tratarerro

 }// fim da claas banco




 ?>

Thank you so much for your help!

  • 3

    Hello, enter the code in the question, not the Pastebin link.

  • @sam E keeps giving error. The object of my problem remains the same.

1 answer

1

connecta is a method of its class, you need to call it with $this->connecta, just like you already do in the builder.

public function executeSQL($sqlexe=NULL){
    if($sqlexe!=NULL):
        $query = mysqli_query($this->connecta(),$sqlexe) or tratarErro(__FILE__,__FUNCTION__);
    else:

    endif;
}
  • I changed it so you can see the error and the code better! Obg!

Browser other questions tagged

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