我正在尝试从数据库中名为usuario的表中获取id值,将$ username作为参数传递,函数$ conexion-> connect()返回一个mysqli对象。这些函数没有给我任何错误但它没有返回数据库中的值。我错过了什么吗?或者犯了什么错误。 谢谢你的帮助。
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;
}
这是函数connect(),它位于类文件“Connection”
中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
}
答案 0 :(得分:1)
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;
}
您应该将新mysqli的返回值存储在变量中,然后使用该变量进行查询或准备。