Jump to content

displaying a users data


kingb00zer
 Share

Recommended Posts

Hi I have recently got to the point in a site im making where I can log in the next step has got me really confused. See what I want to do is after I log in I just want to see a page which says welcome, this is your username, this si your password and this is your email adress. But I want to retrieve that data from the table in the database which stores that information. The table in the database called "users" has the following rows: id (which is auto increment & indexed), username, password and email. I understand I need to use an array and possibaly a loop to get that information but I am also needing to use some kind of session related code to retrieve that users username, password and email which is the part that has me confused. This is what I have so far

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><?php session_start(); ?><html><head><link rel="stylesheet" type="text/css" href="gamestyles.css" /> </head><title> array, session and database test.</title>	<body bgcolor="black">		<?php // need to figure out what to put after WHERE in order to pinpoint the data in the database that matches the user in this session	$user=mysql_query("SELECT username FROM users WHERE");while($row = mysql_fetch_array($user))  {	$username=$row['username'];$pass=mysql_query("SELECT password FROM users WHERE");while($row = mysql_fetch_array($pass))  {	$password=$row['password'];$mail=mysql_query("SELECT email FROM users WHERE");while($row = mysql_fetch_array($mail))  {	$email=$row['email']; } ?>			<h1 align="center" class="one"> Welcome. </h1>		<table class="mainpage" width="240px">		<tr><td class="two" align="left"> Your username is: $username		</td></tr>		<tr><td class="two" align="left"> Your password is: $password		</td></tr>		<tr><td class="two" align="left"> Your email is: $email		</td></tr>		</table>			</body>	</html>

just a note, I am aware that bgcolor is depreciated i just havnt got aroudn to add it to style sheet. Also there are 3 variables in the html table that I havn't got within php tags yet thats not a problem I still have to echo out the table I jst wanted to get this question off before i go to bed. thank you

Link to comment
Share on other sites

I assume you have a login-form already. When the user logs in succesfully, you save the username in a session variable e.g. $_SESSION['username']. Then you query the database for all results where username is equal to the username saved in the session variable.SQL:

$sql = "SELECT * FROM users WHERE username='" . $_SESSION['username'] . "'";

Query:

$link = mysqli_connect("host", "username", "password", "database") or die(mysqli_error($link));$query = mysqli_query($link, $sql) or die(mysqli_error($link));$result = mysqli_fetch_array($query);

Now $result is an array and you can echo out its values by their index e.g.echo $result['email'];will echo out the email of the user.

Link to comment
Share on other sites

Thanks I gave it a try and well i couldnt get to see if ti worked lol, this is kind of embarrasing I leared where I'm about to ask where I went wrong at school just last year. I changed the entire file so that it is php with html embedded in echo's and now I'm getting a "syntax error, unexpected $end" on line 38 which is the ?> line. I bet it is something small lol.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><?php session_start(); echo "<html>";echo "<head> <link rel='stylesheet' type='text/css' href='gamestyles.css' /> </head>";echo "<title> array, session and database test. </title>";echo	"<body bgcolor='black'>";		 	$user=mysql_query("SELECT username FROM users WHERE username='" . $_SESSION['username'] . "'");while($row = mysql_fetch_array($user))  {	$username=$row['username'];$pass=mysql_query("SELECT password FROM users WHERE username='" . $_SESSION['username'] . "'");while($row = mysql_fetch_array($pass))  {	$password=$row['password'];$mail=mysql_query("SELECT email FROM users WHERE username='" . $_SESSION['username'] . "'");while($row = mysql_fetch_array($mail))  {	$email=$row['email']; } 	echo	"<h1 align='center' class='one'> Welcome. </h1>";echo	"<table class='mainpage' width='240px'>";echo	"<tr><td class='two' align='left'>Your username is: $username </td></tr>";echo	"<tr><td class='two' align='left'>Your password is: $password	</td></tr>";echo	"<tr><td class='two' align='left'>Your email is: $email	</td></tr></table>";echo	"</body>";	echo	"</html>";?>

Link to comment
Share on other sites

$user=mysql_query("SELECT username FROM users WHERE username='" . $_SESSION['username'] . "'");while($row = mysql_fetch_array($user)) { $username=$row['username'];$pass=mysql_query("SELECT password FROM users WHERE username='" . $_SESSION['username'] . "'");while($row = mysql_fetch_array($pass)) { $password=$row['password'];$mail=mysql_query("SELECT email FROM users WHERE username='" . $_SESSION['username'] . "'");while($row = mysql_fetch_array($mail)) { $email=$row['email'];}
Here there is some problem in braces sequence in while loops.You can ommit the multiple while loops.You can use just one query to get all of the data password,email,username if you want..
mysql_query("SELECT email,password,username FROM users WHERE username='" . $_SESSION['username'] . "'");

More info here http://w3schools.com/php/php_mysql_select.aspYou are trying to print the password from the database. Password should be encrypted which you cant show it this way. This is not the reason for this error but its good practice. I guess you are storing the user inputed password directly. You should encrypt the password when you store it at the time of registration.You may want to check http://php.net/function.md5You can also ommit the username column from the query.Cause the username is alread stored in $_SESSION after authentication at the time of login. you can simply print the $_SESSION['userid'] if you want.session_start() should be executed first before any print has been sent to browser. even a printing a blank space. here you first printing this first.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
It will going to show you a warning soon... header already been sent.... Edited by birbal
Link to comment
Share on other sites

<?php session_start(); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><link rel="stylesheet" type="text/css" href="gamestyles.css" /><title>array, session and database test.</title></head><style>body{ 		background: #000000;}</style><body><?php$link = mysql_connect("host", "user", "password");mysql_select_db("database", $link);$SQL = "SELECT * FROM users WHERE username='" . $_SESSION['username'] . "'";$query = mysql_query($SQL);$result = mysql_fetch_array($query);	echo	"<h1 align='center' class='one'> Welcome. </h1>";echo	"<table class='mainpage' width='240px'>";echo	"<tr><td class='two' align='left'>Your username is: " . $result['username'] . " </td></tr>";echo	"<tr><td class='two' align='left'>Your password is: " . $result['password'] . " </td></tr>";echo	"<tr><td class='two' align='left'>Your email is: " . $result['email'] . " </td></tr></table>";?></body>   </html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...