Error "expects Parameter 1 to be mysqli, string Given in"

Asked

Viewed 1,351 times

6

I am creating an Insert form for registration, but when I click on the button it is showing me error:

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

I wonder what’s going on?

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>

php connection.:

<?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.');

Could someone help me? Thank you...

2 answers

12


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

You do:

$bd="admin_site";

And try to use

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

Since the first parameter must be the connection, and not the DB name.

The right thing would be:

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

Suggested improvement:

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

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

and then:

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

8

The problem is in:

$result = mysqli_query($bd, $sql)

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

According to the documentation an expected link as the function’s first argument mysqli_query

Browser other questions tagged

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