I cannot resolve this mysqli_query() error

Asked

Viewed 69 times

0

I’m doing a tutorial that is in an old version of PHP, a Twitter clone. I have now, two names, one that connects to the database and the record page. Follow the codes and within the records page has a possibility of response that I do not know how to solve. I wonder if someone could help me?

bd.class.php

<?php

class bd{
    //host
    private $host = 'localhost';
    //usuario
    private $user = 'root';
    //senha
    private $password = '';
    //banco de dados
    private $database = 'twitter_clone';

    //Conectando ao Banco de dados
    public function conecta_mysql(){
        //caso não haja conexão exibir o erro
        $con = mysqli_connect($this -> host, $this -> user, $this -> password) or die("Erro ao conectar ao servidor: ".mysql_error());
        //caso não encontre o banco de dados exibir o erro
        mysqli_select_db($con, $this -> database) or die('Erro ao selecionar o banco de dados'.mysql_error());
        //usando os mesmos caracteres
        mysqli_query($con, "SET NAMES 'utf8");
        mysqli_query($con,"SET 'character_set_connection=utf8");
        mysqli_query($con, "SET 'character_set_client=utf8");
        mysqli_query($con, "SET 'character_set_results=utf8");
        return $con;

    }
}   

?>

registra_usuario.php

<?php

require_once('bd.class.php');

$usuario = $_POST['usuario'];
$email = $_POST['email'];
$senha = $_POST['senha'];

//instância da classe bd
$objBd = new bd();
$objBd -> conecta_mysql(); //conectando ao bd

//Inserindo registros
$sql = "insert into usuarios(usuario, email, senha)values('$usuario', '$email', '$senha')";

//Conferindo o registro
if(mysqli_query($sql)){
    echo 'Usiário registrado com sucesso';
} else {
    echo "Erro no registro";
}

// tentei fazer do jeito a baixo, mas nãos ei como faço para chamar a variável dentro da função no arquivo bd.class.php
// $query = mysqli_query($con, $sql);


?>

The error that appears is:

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\twitter_clone\registra_usuario.php on line 17```
  • the function mysqli_query() waits for two parameters and you only passed one, you entered the query($sql) but did not pass the connection.

  • i searched this, but when I call the connection variable that is within the function in the other file presents another error.

1 answer

0


The mysqli_query in its procedural style requires you to enter the connection link in the first parameter. Reference: Manual PHP - mysqli query

In your case I believe the code would look like this:

if(mysqli_query($objBd->conecta_mysql(), $sql)){
    echo 'Usuário registrado com sucesso';
} else {
    echo "Erro no registro";
}
  • Thank you!!!! worked out! Pity my vote does not compute for you :/ still to a low level

  • to mark the answer as correct I think, if you give a lot of thanks =)

Browser other questions tagged

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