Retrieve mysqli_fetch values

I am trying to get the id value from the usuario table in the database, passing $ username as a parameter, the function $ conexion-> connect () returns a mysqli object. The functions do not give me any errors, but do not return a value from the database. Am I missing something? or make some mistake. Thanks for the help.

public function checkUserNameExists($username){ $conexion = new Connection(); $conexion->connect(); $query = "select id from usuario where username = ?"; $reg = 0; $stmt= $conexion->connect()->prepare($query); $stmt->bind_param('s',$username); $stmt->execute(); $stmt->bind_result($id); while($stmt->fetch()){ $reg = $id; } $stmt->close(); return $reg; } 

This is the connect () function, which is located in the "Connection" class file

 public function connect(){ $mysqli = new mysqli($this->db_host,$this->db_user,$this->db_pass,$this->db_name); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } return $mysqli } 
+5
source share
1 answer
 public function checkUserNameExists($username){ $conexion = new Connection(); $conn = $conexion->connect(); $query = "select id from usuario where username = ?"; $reg = 0; $stmt= $conn->prepare($query); $stmt->bind_param('s',$username); $stmt->execute(); $stmt->bind_result($id); while($stmt->fetch()){ $reg = $id; } $stmt->close(); return $reg; } 

You must store the return value of the new mysqli in a variable, and then use this variable to execute queries or prepare.

+1
source

All Articles