PHP - SQL: show the user’s email from the database

Asked

Viewed 241 times

-1

Guys, here’s the deal: Made the user registration on the site, it saves in the database (obvious).

This is the code of the connection to the database (this is correct?):

<?php      
echo $sql= "select *  from users order by email";
$rs =mysql_query($sql,$connection) or die ("Consulta nao realizada");
?>

The question is how to make the bank take only the email of the active user and not show the email of all users of the site.

I want to show the email of the active user with a "echo $email" and do not know where and how to appear.

Thank you!

  • That code is obsolete only.

  • What do you mean? It’s that I don’t know how to "capture" only the active user email and show you the page.

  • When you log in, I imagine you should save this information in the session, right?

  • yes, it keeps in users

2 answers

0


I recommend using PDO, Mysql native functions for PHP are decrypted for years.

But answering your question, you do not need the second parameter. Considering that a connection to the database is open just use

$query = mysql_query('select *  from users order by email');

And to return the data just use the mysql_fetch_assoc

while ($row = mysql_fetch_assoc($query)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

I recommend reading the full documentation of mysql_query clicking here.

  • Thank God for Vinicios! After just adding this code that you sent, it appears all on the same line, like admin password... one next to the other, and I just wanted to show the email of the active user.

  • 1
    1. Now it only appears the emails added by all registered users, I wanted it to show only the email of the logged-in user
  • Use a where id = id_usuario_logado in your consultation with suggested @Filiwoe

0

recommend using Sesssions Voce will have to query Mysql by filtering the id of the logged-in user.

$sql = "Select email from users where id = $id"; //id = a chave primária da tabela de usuarios
$query = @mysql_query($sql);
$resultado = @mysql_fetch_assoc($query);

done this is now create the Session and set its value

$_SESSION['usuarioID'] = $resultado['id']; //exemplo de outros dados que queira
$_SESSION['usuarioEmail'] = $resultado['email'];

to show the email

$pEmail = $_SESSION['email']; 

echo $pEmail;
  • Thanks Filipe, you helped a lot!

Browser other questions tagged

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