problems with perl session variables

1

I have the following code and I do not understand why it does not save the data of the rows after executing the query.

#!"C:\xampp\perl\bin\perl.exe" 
use Digest::MD5 qw(md5 md5_hex md5_base64);

use CGI;
use CGI::Session(); #Módulo para la sesión
use CGI::Carp qw(fatalsToBrowser); #Muestra posibles errores en el navegador
use DBI; #Conector para la base de datos
use CGI::Session;
$session = CGI::Session->new();
$CGISESSID = $session->id();
#Se crea el objeto CGI
my $cgi = new CGI;

### Recibimos la clave y el valor de los parámetros

my $email = $cgi->param("Email", $cgi->param("Email"));
my $email2 = $cgi->param("Email2", $cgi->param("Email2"));


### Datos de conexión a la base de datos

my $admin = "root";
my $pass = "";
my $base_de_datos = "amaclone";
my $servidor = "localhost";

### Se establece la conexión a la base de datos mysql

my $mysql = DBI -> connect("DBI:mysql:$base_de_datos;host=$servidor", $admin, $pass) || die "Error al conectar con la base de datos: $DBI::errstr";

### Preparamos la consulta para seleccionar datos

my $consulta = $mysql->prepare("SELECT * FROM user_info WHERE email='$email' AND password='$email2'") || die "Error al seleccionar datos: $DBI::errstr";

#Se ejecuta la consulta

$consulta->execute();

### Se busca si existe la fila con tal usuario y password

my $encontrar = 0;
while ($consulta->fetch())
{
    $encontrar = 1;
}

### Si la fila existe

if ($encontrar == 1)
{
    print $cgi->header(-type=> 'text/html', -charset=>'utf-8');
    $datos=$consulta->fetchrow_arrayref();
    print "Bienvenido usuario $email,sus datos son correctos";
    print "Código:$datos->[0] Nombre :$datos->[1]\n";
}
else
{
    print $cgi->header(-type=> 'text/html', -charset=>'utf-8');
    print "sus datos son incorrectos";
    print "SELECT * FROM user_info WHERE email='$email' AND password='$pass'";
}
    
asked by Carlos Jose Bonilla 21.10.2017 в 03:42
source

1 answer

1

The fetch () method is an alias of the fetchrow_arrayref () method.

What is happening is that the while () that you use to set the variable $ find to 1, exhausts the entire response to the query, so afterwards, when you want to do another fetchrow_arrayref (), it no longer returns anything.

What you have to do is store the result in the first while (). For example, it would be something like this (not tested):

my $datos_ref;          # referencia a array de arrays

while (my $row_ref = $consulta->fetch()) {
    push @{$datos_ref},  $row_ref;
}

And you already have in $ data_ref the results matrix. You only have to change the if () to

if (@$datos_ref) {          # sí hay resultados

But, remember, it's a matrix, not a row. You must go through the first level array, and each of them will be another (reference to) array.

for my $row_ref (@$datos_ref) {
    print "Código:$row_ref->[0] Nombre :$row_ref->[1]\n";
}
    
answered by 21.10.2017 в 05:05