Php 检索值mysqli_fetch

Php 检索值mysqli_fetch,php,mysqli,Php,Mysqli,我试图从数据库中名为usuario的表中获取id值,将$username作为参数传递,函数$conexion->connect()返回一个mysqli对象。这些函数没有给我任何错误,但它不会从数据库返回值。我错过什么了吗?或者犯任何错误。 谢谢你的帮助 public function checkUserNameExists($username){ $conexion = new Connection(); $conexion->connect(); $query =

我试图从数据库中名为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”中

您应该将new mysqli的返回值存储在一个变量中,然后使用该变量进行查询或准备

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
}
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;
}