Error "expects parameter 1 to be mysqli, string given in"

5

I am creating an insert form to register, but when I click the button it is presenting me an error:

  

Warning: mysqli_query () expects parameter 1 to be mysqli, string given in C: \ wamp64 \ www \ Project Beauty and Harmony \ Beauty and Harmony - site \ php \ cadastro.php on line 25

What's going on?

cadastro.php:

<html>
<head>
    <title>cadastro concluido</title>
    </head>

    <body>


<?php 
include("../php/conexao.php");


$nome = $_POST['nome'];
$ende = $_POST['endereco'];
$tel = $_POST['telefone'];
$comple = $_POST['complemento'];
$login = $_POST['login'];
$senha = $_POST['senha'];
$mail = $_POST['emails'];
$nume = $_POST['numero'];


$sql = "INSERT INTO usuario(nome, telefone, endereco, numero, complemento, email, login, senha) VALUES('$nome', '$tel', '$ende', '$nume', '$comple', '$mail', '$login', '$senha')";

        $result = mysqli_query($bd, $sql) or die ('Error querying database.');

        mysqli_close($bd);  
?>

    </body>

</html>

connection.php:

<?php 

$host = "localhost";
$usuario = "root";
$senha = "";
$bd="admin_site";



$mysqli = new mysqli($host, $usuario, $senha, $bd);


if($mysqli -> connect_errno) {
    echo "Falha na conexão: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
}


?>

line 25:

 $result = mysqli_query($bd, $sql) or die ('Error querying database.');

Can anyone help me? Thank you ...

    
asked by anonymous 05.01.2017 / 01:39

2 answers

10

Virtually all mysqli functions use the "connection" link to the database as the first parameter.

You do:

$bd="admin_site";

And try to use

$result = mysqli_query($bd, $sql);

The first parameter should be the connection, not the DB name.

The correct one would be:

$result = mysqli_query( $mysqli, $sql );

Suggested improvement:

In order not to make a bad mess, I suggest changing the link / connection name to $con or $link , which are very common in documentation and examples:

$link = new mysqli($host, $usuario, $senha, $bd);

then:

$result = mysqli_query( $link, $sql );
    
05.01.2017 / 01:46
8

The problem is:

$result = mysqli_query($bd, $sql)

You are passing the value of $db as a parameter, but $bd is only a string , tries to pass as parameter the $mysqli

According to documentation is expected link as the first argument of the function mysqli_query

    
05.01.2017 / 01:49