Display Welcome Screen with User Name in PHP page

Asked

Viewed 198 times

0

Good afternoon. I’m trying to implement on a page PHP a welcome message after a login. I would like to display the name of the user who has just logged in through a variable php. The login screen requires the email and password registered in the database. I stored each of the two in a SESSION to authenticate. On the following page I want the NAME, I know that it is necessary to search for this name in the database passing as parameter the email that the user logged in and storing in a variable (query mysql). I tried to do so:

 $busca = "SELECT Nome FROM newsletter WHERE Email = '$_SESSION['Email']'";
 $result = mysqli_query($conexao, $busca) or die("Falha");

Compiler returns the error:

Parse error: syntax error, Unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting Identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C: xampp htdocs Blog php areavip.php on line 14

I managed to get around this error by taking this SESSION and storing it in another variable, leaving the code like this:

$email = $_SESSION['Email'];
$busca = "SELECT Nome FROM newsletter WHERE Email = '$email'";
$result = mysqli_query($conexao, $busca) or die("Falha");

The problem is when I used an echo to display what was stored in the variable it returns:

SELECT Nome FROM newsletter WHERE Email = '[email protected]'.

The email has been stored, but it returns every query as a string. Before using this command I tested it in Workbench to see if the return of the search was expected and there ta all right the problem is somewhere in this script but I could not find.

2 answers

0

Josafa will be that you can post the code you are using to give echo? You need to do something like this:

$email   = $_SESSION['Email'];
$busca   = "SELECT Nome FROM newsletter WHERE Email = '$email'";
$result  = mysqli_query($conexao, $busca) or die("Falha");    
$row     = mysqli_fetch_assoc($result);

echo $row['Nome'];

0

Good afternoon Lucas, thank you so much for your reply, I found the error I made, I forgot to use the fetch function. After declaring the variable $result and already wrote echo $search , so it was returned all as a string. I already managed to fix my code here. Thank you so much for the help.

Browser other questions tagged

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