Good afternoon, I have the following question: Why is not the value in the column I want to access displayed on the screen?
I have this PHP code with which I verify the login of a user.
public function verificarUsuario($nombre_usuario,$password_usuario){
$modelo = new Conexion();
$conexion = $modelo->get_conexion();
$sql = "select nickname, password
from usuarios
where nickname = :nombre_usuario
and password = :password_usuario";
$statement = $conexion->prepare($sql);
$statement->bindParam(':nombre_usuario', $nombre_usuario);
$statement->bindParam(':password_usuario', $password_usuario);
if(!$statement){
return "Error verifique parametros";
}else{
$statement->execute();
if($statement->rowCount() > 0){
return $statement->fetchAll(PDO::FETCH_ASSOC);
}else{
return "no hay usuarios con registrados con esos datos";
}
}
}
That according to the PHP manual (PDO :: FETCH_ASSOC) returns an associative Array.
PDO :: FETCH_ASSOC: returns an array indexed by the names of the columns in the result set.
Now here I make the call to the file that contains the method, and I execute it.
<?php
require_once('../Modelo/class.conexion.php');
require_once('../Modelo/class.consultas.php');
session_start();
$usuario = $_POST['var1'];
$pass = $_POST['var2'];
if(strlen($usuario) > 0 && strlen($pass) > 0){
$consultas = new Consultas();
$mensaje = $consultas->verificarUsuario($usuario, $pass);
print_r($mensaje);
for($i=0;$i<count($mensaje);$i++) {
echo $mensaje['nickname'].'<br />';
}
}else{
echo "Por favor rellena ambos campos";
}
?>
with print_r($mensaje);
I get the following in the message variable:
Array ([0] => Array ([nickname] = > user1 [password] = > user12345))
But then when I try to go through it with the cycle for
, it displays the following message.
Notice: Undefined index: nickname in C: \ xampp \ htdocs \ TokioGhoul \ Driver \ login.php on line 15
Obviously line 15 is where I try to echo the column I want to access.
for($i=0;$i<count($mensaje);$i++) {
echo $mensaje['nickname'].'<br />'; <- Aqui se da el error
}
All this I do in order to keep the session of a user who enters.
For your attention, thank you.