Call to a Member Function bind_param()

Asked

Viewed 1,026 times

2

I’m a beginner in PHP and I’m getting this error while trying to make a Insert in the database using Mysqli functions:

Fatal error: Call to a member function bind_param() on string in C:\xampp\htdocs\Uc16\cadastro.php on line 5

What I’m doing wrong?

        $sql = "INSERT INTO tb_usuarios(user_nome,user_senha,user_nivel,user_cpf,user_end_rua,user_end_bairro,user_end_numero,user_tel,user_email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        $sql->bind_param('ssisssiss',
        $_POST['user_nome'], 
        $_POST['user_senha'],
        $_POST['user_nivel'],
        $_POST['user_cpf'],
        $_POST['user_end_rua'],
        $_POST['user_end_bairro'],
        $_POST['user_end_numero'],
        $_POST['user_tel'],
        $_POST['user_email']);
        if ($conn->query($sql) === TRUE) {
            echo "Registrado";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

        $conn->close();

     ?>

  • 2

    The bind_param can only be used in a stmt and not in a string. So the $sql must be a stmt, for example a mysqli_prepare().

1 answer

2


Could not bind a string, it should be done in the connection variable. First, it should be called prepare()

$sql = "INSERT INTO tb_usuarios(user_nome,user_senha,user_nivel,user_cpf,user_end_rua,user_end_bairro,user_end_numero,user_tel,user_email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conexao->prepare($sql);
$stmt->bind_param(...);
if(!$stmt->execute()){
   echo 'erro: '. $conexao->error;
}
  • but ai da esse erro: Warning: mysqli::prepare() expects Exactly 1 Parameter, 0 Given in C: xampp htdocs Uc16 cadastro.php on line 5

  • 1

    @Igoroliveira no preapre() you should pass the query, corrected that in the reply now.

  • It worked but I’m having sql syntax error, you know what can be?

  • @Igoroliveira needs to see where is the error start indication, can be a wrong column name etc.

  • right syntax to use near '?, ? ? , ? , ? , ? , ? , ? , ? , ? )' at line 1

  • 1

    @Igoroliveira when you use the prepare() does not call the query() but the execute() edited the answer.

  • It worked, thank you!

Show 2 more comments

Browser other questions tagged

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