Parameter error in mysqli_query (does not accept connection parameter)

Asked

Viewed 43 times

0

Good afternoon, everyone

I’m having an error: Warning: Notice: Undefined variable: mysqli in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59

Warning: mysqli_query() expects Parameter 1 to be mysqli, null Given in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59

Warning: mysqli_error() expects Exactly 1 Parameter, 0 Given in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59

public $host      = "localhost";
public $user     = "root";
public $password = "xxxxxxxx";
public $db       = "crud";
public $dataset  = NULL;
public $linhasAfetadas = -1;

public function __construct() {
    $this->conecta();
}

public function __destruct() {

}

public function conecta() {
    $mysqli = new mysqli($this->host, $this->user, $this->password, $this->db);
    if ($mysqli->connect_error) {
        die('Erro de conexão (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
    } 
}

public function inserir($objeto) {
    $sql = "INSERT INTO " . $objeto->tabela . " (";
    for($i = 0; $i < count($objeto->campos_valores); $i++) {
        $sql .= key($objeto->campos_valores);

        if($i < (count($objeto->campos_valores)-1)) {
            $sql .= ", ";
        } else {
            $sql .= ") ";
        }
        next($objeto->campos_valores);
    }
    reset($objeto->campos_valores);
    $sql .= "VALUES (";
    for($i = 0; $i < count($objeto->campos_valores); $i++) {
        $sql .= is_numeric($objeto->campos_valores[key($objeto->campos_valores)]) ?
            $objeto->campos_valores[key($objeto->campos_valores)] :
            "'" . $objeto->campos_valores[key($objeto->campos_valores)] . "'";

        if($i < (count($objeto->campos_valores)-1)) {
            $sql .= ", ";
        } else {
            $sql .= ") ";
        }
        next($objeto->campos_valores);
    }

    echo $sql;
    return $this->executaSql($sql);
}

public function executaSql($sql = NULL) {

        $query = mysqli_query($mysqli, $sql) or die(mysqli_error());
}

}

  • 1

    Missed passing the connection to the mysqli_query() he is always the first argument.

  • 1

    The syntax is $query = mysqli_query($conexao,$sql), more details in the linked post in the closing.

  • I did so and it gave the following error: Notice: Undefined variable: conexao in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59 Warning: mysqli_query() expects Parameter 1 to be mysqli, null Given in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59 Warning: mysqli_error() expects Exactly 1 Parameter, 0 Given in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59

  • 1

    You probably didn’t pass the variable to your method, right? The site is a cool way to take away programming doubts, but is no longer as suitable as "remote Debugger". Basically the error says $mysql is null, that is, you did not pass this parameter in your function (and did not use a class member instead).

  • Okay, I’ll try to see where the mistake is.

  • 1

    The problem is $mysqli(the connection) in the method conecta(), she is a local variable or when concta() finish excutar it will no longer exist. The solution I recommend is to make $mysqli a class member (like $host, $user etc ...) and function calls mysqli_ do as follows: mysqli_query($this->mysqli, $sql)

  • 1

    Marcelo @rray’s solution is well suited, and should be simple to implement, but if you have difficulty, leave a comment here. Basically you will use for example a public $mysqli at the beginning of the code, and in place of the $mysqli existing, exchanged by the member $this->mysqli.

  • PS: ta missing connection here tb: or die( mysqli_error($this->mysqli) );

Show 3 more comments
No answers

Browser other questions tagged

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