List data with SQL Prepared Statements

0

I need to bring the data from the Database using an id. The Users class has the following function:

public function getPerfil($id) {
    $stmt = $this->mysqli->prepare("SELECT id_usuario,nombre,apellido FROM usuarios WHERE id_usuario = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->bind_result($idDB, $nombreDB, $apellidoDB);
    while($row = $stmt->fetch()) {
        $data[] = $row;
    }
    return $data;

    $stmt->close();
    $this->mysqli->close();
}

In the Database I have the column: id, first and last name and I want to show your data by calling them from another file:

session_start();
require_once "clases/Usuarios.php";

$perfil = new Usuarios();
$datos = $perfil->getPerfil(1);

echo $datos->id;
echo $datos->nombre;
echo $datos->apellido;

And I get an error:

  

Notice: Trying to get property of non-object

Is there any way to show the data of the BD through these queries? I can see them correctly if I show them inside the While of the query, for example: echo $nombreDB; but I do not want them there. I want to call them from another file to show where I want.

    
asked by fed R 16.02.2017 в 23:17
source

1 answer

3

Update

Since in the comments you said that you only want to show the name, I modify the optimized code:

public function getPerfil($id) {
    $stmt = $enlace->prepare("SELECT name FROM users WHERE id = ?");
    $stmt->bind_param( "i", $id); 
    $stmt->execute();
    $stmt->bind_result($datos);
    $stmt->store_result();
    $stmt->fetch();
    $stmt->close();

    return $datos;
}

And to obtain your chain you can do it by means of a variable:

$id=1 //aquí el valor del id;
$nombre=getPerfil($id);
echo $nombre;

You can also do the echo directly:

echo getPerfil($id);

Here you have a functional example of the code that you can try by clicking where it says: Execute.

Final note You can improve the getProfile () method for example by asking if the query does not return data and sending a message saying that no data was found in case the query does not get results.

I have seen that you are not really receiving an array from your getProfile () function, however it is ideal, especially if you want to present your data from another location.

The getProfile ($ id) function should look something like this:

public function getPerfil($id) {
    $stmt = $this->mysqli->prepare("SELECT id_usuario,nombre,apellido FROM usuarios WHERE id_usuario = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();

    /* 
    *No tiene sentido usar todo el código comentado si usarás 
    * tus resultados en otro lugar, simplemente obtienes un array 
    * y lo devuelves con return...  
    */

/*    $stmt->bind_result($idDB, $nombreDB, $apellidoDB);
    while($row = $stmt->fetch()) {
        $data[] = $row;
    }*/
/* array asociativo */
    $result = $stmt->get_result(); 
    $datos = $result->fetch_array(); 
    return $datos;
    $stmt->close();
    $this->mysqli->close();
}

Then you can read from the other side as I indicate below, with the foreach

Now yes ...:)

If you look closely, from your function getProfile ($ id) you get an arrangement, therefore, to read $ data you have to iterate:

    if ($datos){        
        foreach($datos as $dato) {
                echo $dato["id"];  //¿id o id_usuario?
                echo $dato["nombre"];
                echo $dato["apellido"];
        }
    } else {
        echo "No se encontraron datos";
    }

You can check what's in $ data by doing:

print_r($datos);
    
answered by 17.02.2017 / 03:10
source