Problem with prepared statement when obtaining several records and saving them in an array - PHP

1

The problem I have is that I do not know if I'm wrongly implementing the fetch_assoc or keeping the records wrong in the ' array because when checking if the $ stmt- & gt query ; execute () fails, tells me that it is correct and does not return anything.

The query should return more than 2 records and then manage them in another file where I will send them to android using echo with json_encode .

public function searchMiembrosGrupo($id_grupo){
    $stmt = $this->conn->prepare("SELECT 'email_padre','nombre_hijo' FROM 'hijos' INNER JOIN 'user' WHERE 'hijos'.'email_padre' = 'user'.'email' AND 'user'.'grupo' = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();

    while ($row = $stmt->get_result()->fetch_assoc()) {
          $miembros_grupo[] = $row;

    }
    return $miembros_grupo;

}

SearchMember.php

<?php 

require_once 'include/DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['id_grupo'])) {

// receiving the post params
$id_grupo = $_POST['id_grupo'];

// get the user by email and password
var_dump($user = $db->searchMiembrosGrupo($id_grupo));
//$user = $db->searchMiembrosGrupo($id_grupo);

if ($user != false) {
    // user is found
    $response["error"] = FALSE;
    $response["user"]["email_padre"] = $user["email_padre"];
    $response["user"]["nombre_hijo"] = $user["nombre_hijo"];
    echo json_encode($response);
} else {
    // user is not found with the credentials
    $response["error"] = TRUE;
    $response["error_msg"] = "Los datos estan mal. Por favor intenta nuevamente!";
    echo json_encode($response);
}
} else {
// required post params is missing
$response["error"] = TRUE;
$response["error_msg"] = "Se requiere el dato (id de grupo). Intentalo nuevamente!";
echo json_encode($response);
}


?>
    
asked by Rosyec Parrado 15.02.2018 в 23:57
source

1 answer

1

The problem is in the way you access the results, to use a prepared statement you have two alternatives, to binde the fields or get the result and ask for the arrays for the rows:

The correction would be that you should ask for the result only once, so

public function searchMiembrosGrupo($id_grupo){

        $stmt = $this->conn->prepare("SELECT 'email_padre','nombre_hijo' FROM 'hijos' INNER JOIN 'user' WHERE 'hijos'.'email_padre' = 'user'.'email' AND 'user'.'grupo' = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $result = $stmt->get_result();
        $miembros_grupo = array();

        while ($row = result->fetch_assoc()) {
            array_push($miembros_grupo,$row);
        }
        return $miembros_grupo;
    }

Or if you can not match the results, like this:

public function searchMiembrosGrupo($id_grupo){
    $stmt = $this->conn->prepare("SELECT 'email_padre','nombre_hijo' FROM 'hijos' INNER JOIN 'user' WHERE 'hijos'.'email_padre' = 'user'.'email' AND 'user'.'grupo' = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $stmt->bind_result($email_padre,$nombre_hijo);

    $miembros_grupo = array();

    while ($stmt->fetch()) {
        $row = arraw("email_padre" => $email_padre, "nombre_hijo" => $nombre_hijo);
        array_push($miembros_grupo,$row);
    }
    return $miembros_grupo;
}

Both are equally valid, personally I prefer the second if I do not need the result object for anything particular and I only intend to use the data of the fields. I hope you have solved your problem

    
answered by 16.02.2018 / 00:04
source