PHP Query Error

Asked

Viewed 68 times

-1

I’m trying to make a file in php, which refers to the person’s age and then shows, however, the $login appears to me but the person’s $age does not. I wonder what I’m doing wrong.

<?php
    include ("sistema/validar_session.php");
    include("config.php");

    $login = "Goncalo";
    $select = mysql_query("SELECT * FROM dados_usuarios WHERE Login = '$login'");
    $consulta = mysql_fetch_array($select);
    $idade = $consulta['idade'];

    echo "Olá $login a sua idade é de $idade";
 ?>

Here is the table structure:

  • Show as this the table of your Database where shows the User Login information

  • Here is the print: http://prntscr.com/7iahxo

  • can find the error?

  • 1

    Gonçalo, enter the relevant code snippet in the body of the question as text. Click [Edit] to do so

  • In that case it seems to have no value for idade in this record, I see no errors in the code.

  • 1

    Is the record being located? place a var_dump($consulta;) at the end of the code to see what appears.

Show 1 more comment

2 answers

0

Hello, probably your query is not bringing results

<?php 
include ("sistema/validar_session.php"); 
include("config.php");

$login = "Goncalo";


$select = mysql_query("SELECT * FROM dados_usuarios WHERE Login = '$login'");

if( mysql_num_rows ( $select ) == 0 )
   echo 'O login '.$login.' não foi encontrado';
else
{
   while ($row = mysql_fetch_assoc($select))
     echo "Olá ".$login." a sua idade é de ".$row['idade'];
}
?>

-3

The best would be to put:

<?php include ("sistema/validar_session.php"); include("config.php");

$login = "Goncalo";


$sql= mysql_query("SELECT * FROM dados_usuarios WHERE Login = '$login'");

$row= mysql_fetch_array($sql);

$idade = $row['idade'];

echo "Olá $login a sua idade é de $idade"; ?>
  • 2

    You just changed the name of the variable

Browser other questions tagged

You are not signed in. Login or sign up in order to post.