Good Morning. I have the following error: Fatal error: Uncaught Error: Call to a member function bind_param()
I am trying to use a prepared statement for a SELECT, but they do not accept it, instead if I do it I do it using variables, if it accepts it and I get the expected result.
The data that I want to parameterize is an integer.
The statement is correct, since if it were not correct, it would not work out well for me, not even putting the variable instead of the ?
public function mostrarPerfil($nia){
$sql = "SELECT * FROM usuario WHERE nia = ?";
$result = $this->conn->query($sql);
$result->bind_param('i',$nia);
$result->execute();
if ($result->num_rows > 0) {
$usuario = $result->fetch_all(MYSQLI_ASSOC);
return $usuario;
}
$result->close();
$this->conn->close();
}
It sounds like you're mixing prepared and unprepared query methods. I give you two options:
not ready
ready
Note that the
query()
'equivalent' toprepare()
,bind_param()
andexecute()
, you need to declare in which variable you are going to save the result withbind_result()
and, in eachfetch()
, you are updating the value of the variable$nombre
.It is more laborious, but safer.
In php.net you have the details:
https://www.php.net/manual/en/class.mysqli.php
https://www.php.net/manual/en/mysqli.prepare.php
Prepared with more fields