Page 1 of 1

Displaying Info for the person logged in Rate Topic: -----

#1 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Post icon  Posted 31 December 2009 - 06:31 PM

Im gonna show you how to show info or anything that's stored in the database
For the user who is logged in

Now lets get started Most login script use the variable username ( some use userNAME )

This is made when the user logs in so we know whos who

it looks a bit like this
$username



Now first thing we got to do is start the session and connect to the database

session_start();

mysql_connect("host", "username", "password") or die(mysql_error());
mysql_select_db("database name") or die(mysql_error())



The my sql username and password you make / get when you make a new database

Now we query the database about the column we want and which table its in


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

$res = mysql_query($sql)  or die('Invalid query: ' . mysql_error());

$val = mysql_fetch_array($res);



line 1...now we are telling it to select the username column from the database from the users table

The were bit is telling it to only show the username of the person logged in
if we have set the $username ( which most login script do )

line 2...Then we tell it that if there is a error with any off it to show a error messege

Line3 is just telling it to fetch the info we want ( the username in this case)

now we display the username on the page by echoing it out

echo $val['username'];



so now here is the code in fill


mysql_connect("host", "username", "password") or die(mysql_error());
mysql_select_db("database name") or die(mysql_error())

$sql = "SELECT username from users WHERE username='".$_SESSION['username']."';";
$res = mysql_query($sql)  or die('Invalid query: ' . mysql_error());
$val = mysql_fetch_array($res);
echo $val['username'];



You can do this with other stuff has well not just username's e.g gender email address anything that is being stored :)

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying Info for the person logged in

#2 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Posted 15 January 2010 - 09:54 AM

Just a random question, right near the beginning you are saying that you need to use session_start();. Yet when you list the code in full it doesn't show it?
Was This Post Helpful? 0
  • +
  • -

#3 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Posted 16 January 2010 - 02:10 PM

this tutorial seems to be something you took from one of your scripts to make into a tutorial. however like auzzie said the is no session_start(); in your full code and you also do not set $_SESSION['username'], thus assuming that it is set upon accessing the site.

theres a few other minor things wrong with it like there is no point using mysql_fetch_array when mysql_result would suffice however im sure there mostly personal preferences. apart from that good tut. it will help begginers quite a bit
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1