When making the query to count the users, these errors appear, this query must enter the users repository and establish the data of each user that there is, if I do a var_dump I get the following:
array(1) { [0]=> array(12) { ["ID"]=> int(1) [0]=> int(1) ["NAME"]=> string(6) "Ishmael" [ 1]=> string(6) "Ismael" ["EMAIL"]=> string(30) "[email protected]" [2]=> string(30) "[email protected]" ["PASSWORD"] => string(4) "1234" [3]=> string(4) "1234" ["DATE_REGISTRATION"]=> string(19) "2022-06-02 13:09:13" [4]=> string (19) "2022-06-02 13:09:13" ["ACTIVE"]=> int(1) [5]=> int(1) } }
Warning: Undefined array key "id" in C:\xampp\htdocs\test\app\UserRepository.inc.php on line 21
Warning: Undefined array key "name" in C:\xampp\htdocs\test\app\UserRepository.inc.php on line 22
Warning: Undefined array key "email" in C:\xampp\htdocs\test\app\UserRepository.inc.php on line 23
Warning: Undefined array key "password" in C:\xampp\htdocs\test\app\UserRepository.inc.php on line 24
Warning: Undefined array key "date_registration" in C:\xampp\htdocs\test\app\RepositorioUsuario.inc.php on line 25
Warning: Undefined array key "active" in C:\xampp\htdocs\test\app\UserRepository.inc.php on line 26
Page Code:
<?php
include_once "app/Conexion.inc.php";
include_once "app/RepositorioUsuario.inc.php";
Conexion :: abrir_conexion();
//Hacemos que usuarios sea igual a obtener todos, lo alimentamos con la conexion obtener conexion
$usuarios = RepositorioUsuario :: obtenerTodos(Conexion::obtener_conexion());
//Escribir nuemor de usuario para ver si funciona
echo count($usuarios);
Conexion :: cerrar_conexion();
?>
User repository code:
<?php
class RepositorioUsuario{
public static function obtenerTodos($conexion){
$usuarios = array();
if (isset($conexion)){
try {
include_once "Usuario.inc.php";
$sql = "SELECT * FROM usuarios";
$sentencia = $conexion -> prepare($sql);
//Ejecutar sentencia sql
$sentencia -> execute();
//que devuelva los resultados
$resultado = $sentencia -> fetchAll();
var_dump($resultado);
if(count($resultado)){
foreach($resultado as $fila){
$usuarios[] = new Usuario(
$fila["id"],
$fila["nombre"],
$fila["email"],
$fila["password"],
$fila["fecha_registro"],
$fila["activo"]
);
}
} else {
print "No hay usuarios";
}
} catch (PDOException $ex) {
print "ERROR".$ex -> getMessage();
}
}
//Devolvemos el array de usuarios
return $usuarios;
}
}
?>
User.inc.php:
<?php
class Usuario {
private $id;
private $nombre;
private $email;
private $password;
private $fecha_registro;
public function __construct($id, $nombre, $email, $password, $fecha_registro, $activo){
//El atributo de la clase es el mismo al q pasamos al constructor
$this -> id = $id;
$this -> nombre = $nombre;
$this -> email = $email;
$this -> password = $password;
$this -> fecha_registro = $fecha_registro;
$this -> activo = $activo;
}
public function obtenerId(){
return $this -> id;
}
public function obtenerNombre(){
return $this -> nombre;
}
public function obtenerEmail(){
return $this -> email;
}
public function obtenerPassword(){
return $this -> password;
}
public function obtenerFechaRegistro(){
return $this -> fecha_registro;
}
public function esta_activo(){
return $this -> activo;
}
public function cambiarNombre($nombre){
$this -> nombre = $nombre;
}
public function cambiarEmail($email){
$this -> email = $email;
}
public function cambiarPassword($password){
$this -> password = $password;
}
public function cambiarActivo($activo){
$this -> activo = $activo;
}
}
?>
In PHP array key names are case sensitive , meaning that if the key is
ID
in the array, you can't try to read it like this:$elArray['id']
, but put it like this:$elArray['ID']
.That said, you are wasting resources, reading and rereading the data. You can indicate
fetchAll()
that you want an associative array, and use that array, without needing to recreate another array.Furthermore, for this case, since you have a class
Usuario
, you can map the rows to instances of that class, byThat is, instead of this:
You can simplify quite a bit by doing this:
$allUsers
will be an array of objects of typeUsuario
, and to check its status you can do:About mapping results from PDO to classes, you can also refer to this question: How to map the result of an SQL query to a class in PHP using PDO and how to use it? .