Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieve values mysqli_fetch

Tags:

php

mysqli

I'm trying to get the id value from a table called usuario in the database, passing $username as parameter, the function $conexion->connect() returns a mysqli object. The functions give me no errors but it doesn't return the value from database. Am I missing something? or making any mistake. Thanks for 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 function connect() what is located in a class file "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
}
like image 674
ElijahCarrillo Avatar asked Nov 10 '22 10:11

ElijahCarrillo


1 Answers

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 should store the return value of new mysqli in a variable, and then use that variable to make queries or prepares from.

like image 147
Jesper Avatar answered Nov 15 '22 11:11

Jesper