-1

Made user registration on the site, it saves in the database (obvious).

<?php
$query = mysql_query('select email from users where username="admin4"');

while ($row = mysql_fetch_assoc($query)) {
    echo $row["email"];
}
?>

The question is how do to the bank just get the active user email and not show email all users of the site. I want to show the active user and email with an "echo $email" and do not know where and how to appear.

Thank you!

eduardo
  • 11
  • 4
  • You need to add a WHERE clause to your SQL. But since we've no idea how you know who the active user is, there's no way to give more concrete help – andrewsi Jul 13 '16 at 23:20
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Jul 13 '16 at 23:26
  • 2
    This is really SQL 101. And you should be learning this from chapter one of a book. **SO is not a tutorial site** – RiggsFolly Jul 13 '16 at 23:28
  • He seems inexperienced with both PHP & MySQL, so I think we should all try to explain things as much as possible. – Steven Matthews Jul 13 '16 at 23:29
  • I do not have much experience with php. I want to show the email for user not for all users. – eduardo Jul 14 '16 at 00:00

1 Answers1

0

You're selecting all users on the site with your query. You need to modify your query to be something like this:

SELECT * FROM users WHERE username = 'USERNAMEGOESHERE'

or possibly

SELECT email FROM users WHERE username = 'USERNAMEGOESHERE'

Note, this is just an example. I don't know what your username column is named or your email column or what your individual user is named.

The WHERE clause adds a condition that prevents you from getting every single record from the table you're querying.

You do not need the ORDER BY clause in this case, at least if I am correctly understanding your needs.

I don't know how you'll get the active user for the query, you need to pass that in from your session variables or however you are storing that data.

Steven Matthews
  • 9,705
  • 45
  • 126
  • 232
  • Thank you Andrew! But how I can take the logged user to make this application "automatic"? nice!! – eduardo Jul 14 '16 at 00:31
  • Well that's what I was talking about with your session or however you're storing your data for your user when they're logged in. I'm not sure how that part of your application works, and it is separated entirely from this query portion. You probably need to do something like $_SESSION['username'], but I can't know without more information. You should open up a separate question about how to get your active user's username and describe how your users login - everything about how that process works. If you'd like you can tag me and I'll try to answer that too. – Steven Matthews Jul 14 '16 at 22:10
  • Ok, thak you Andrew! – eduardo Jul 15 '16 at 19:49