Problem returning PHP function

Asked

Viewed 42 times

0

Good afternoon, folks I’m starting in php and I’m having a problem with my code

  function buscarIdProduto($conn,$nome){
    $stmt = $conn->prepare("SELECT id FROM produto WHERE NOME_Produto = ?");
    $stmt->bind_param("i", $nome);
    $stmt->execute();
    $resultado = $stmt->get_result();
    return $resultado->fetch_assoc();
  }

in this function I get the product id, the problem is that it always returns the id 1 when I will insert it reference product with id 1.

if(inserirProduto($conn,$idCategoria,$nomeProduto,$descricao,$valor)){
      $produto = buscarIdProduto($conn,$nomeProduto);
}
inserirImagem($conn,$novoNome,$produto["id"],$upload,$tipo,$tamanho);
  • Do bind_param("i", $nome) indicates that $nome will be of the whole type. That’s right? The name wouldn’t be a string?

  • It’s because I actually want the id so I step into the bind an integer

1 answer

2


$stmt->bind_param("i", $nome);

This line is incorrect. You are passing $nome as an integer, but since this variable has an alphanumeric string in it, its function cannot turn letters into numbers.

The right thing to do $stmt->bind_param("s", $nome); or $stmt->bind_value("s", $nome);.
The difference between the two is that bind_param associates the variable to the query, modifying the query when the variable value is modified and bind_value associates the value of the variable to the query, keeping the query the same way even if the variable is modified.

  • That, thank you so much to be able to solve my problem I help a lot.

  • If you can mark the answer as "accept"

Browser other questions tagged

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