smithmr8 Posted January 6, 2008 Share Posted January 6, 2008 Hi, I was recommended this site by someone I know, so I hope you'll be able to help I am currently building a website template site, but am having some difficulty with a members set-up. What I would like it to do.. I would like to be able to retrieve values from a database table using the username of a member. E.g. When someone logs into their account, it will display in the place I would like it..[in the 'Account Information Section', click the link at the base of the post to see where I mean] 'Welcome, USERNAME' I would also be able to get other details retaining to that username, such as its ID [Defined in the MySQL table]. Thanks, Luke The Website: http://www.templateking.co.uk Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/ Share on other sites More sharing options...
revraz Posted January 6, 2008 Share Posted January 6, 2008 Not sure really what you are asking of the forum, but just load those values into a variable and echo them where you want. Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432075 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 We don't write codes for you but if you post what you have we can help you. Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432076 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 You're far better off tracking the user ID rather than a username. Use that to know which user is viewing the page and what information to take from the database. Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432078 Share on other sites More sharing options...
smithmr8 Posted January 6, 2008 Author Share Posted January 6, 2008 I have tried all sorts of things, but have come to no avail. I would like to know how to get the username of the member logged in, and how I can use that to retrieve other values from the table the information is located in. --- I dont want you to write anything for me. Just looking for some guidance really. I am using this as my login script. <?php ob_start(); $host="--REMOVED--"; // Host name $username="--REMOVED--"; // Mysql username $password="--REMOVED--"; // Mysql password $db_name="--REMOVED--"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); if($result){ echo "go"; } //mysql_num_row is counting table row $count=mysql_num_rows($result); //if result match $myusername and mypassword table row must be 1 row if($count==1){ //session_register("myusername"); session_start(); $_SESSION['myusername'] = $myusername; header("location:index.php"); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?> If I try to echo $_SESSION['username'] it just appears as a blank space. Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432081 Share on other sites More sharing options...
revraz Posted January 6, 2008 Share Posted January 6, 2008 First off, put session_start(); as the first line of code after <?php Is there more to this code or is there a form page that calls this? Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432083 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 If I try to echo $_SESSION['username'] it just appears as a blank space. Thats because you used $_SESSION['myusername']; Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432084 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 every page needs session_start(); at the beging to set and see the session's <?php ob_start(); session_start(); $host="--REMOVED--"; // Host name $username="--REMOVED--"; // Mysql username $password="--REMOVED--"; // Mysql password $db_name="--REMOVED--"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); if($result){ echo "go"; } //mysql_num_row is counting table row $count=mysql_num_rows($result); //if result match $myusername and mypassword table row must be 1 row if($count==1){ //session_register("myusername"); $_SESSION['myusername'] = $myusername; header("location:index.php"); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432088 Share on other sites More sharing options...
smithmr8 Posted January 6, 2008 Author Share Posted January 6, 2008 <form method="POST" action="login.php"> <input name="myusername" type="text" size="13" /> <input name="mypassword" type="password" size="13" /> <input type="submit" name="Submit" value="Login!" /> </form> This is the form which calls the script. Located on the index page. ----- I meant to type that . Everytime I try to echo the $_SESSION['myusername'] it simply returns a blank space. Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432090 Share on other sites More sharing options...
smithmr8 Posted January 6, 2008 Author Share Posted January 6, 2008 I've updated the script, so that the session_start() is at the top now. I all added this piece of code to the header file, [it will appear in the section 'Account Information'] <?php if(isset($_SESSION['myusername'])) { echo "Welcome, ". $_SESSION['myusername']; } else { echo "Welcome, Guest"; } ?> Even when I login, it still display's 'Welcome, Guest'.. which indicates something is not working. Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432098 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 Are you sure your password isn't hashed? If so, you need to do that. Not sure what hash you use but I guess md5 right? $mypassword=md5($_POST['mypassword']); Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432114 Share on other sites More sharing options...
smithmr8 Posted January 6, 2008 Author Share Posted January 6, 2008 Not sure what Hashing is, but I added that to the code. Also changed the password in the DB to an md5 version. Still, when I log-in, it does so successfully, but it still wont display the username. Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432123 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 Can you post your updated codes that you have? Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432125 Share on other sites More sharing options...
smithmr8 Posted January 6, 2008 Author Share Posted January 6, 2008 The login.php script <?php session_start(); ob_start(); $host="REMOVED"; // Host name $username="REMOVED"; // Mysql username $password="REMOVED"; // Mysql password $db_name="REMOVED"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $myusername=$_POST['myusername']; $mypassword=md5($_POST['mypassword']); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); if($result){ echo "go"; } //mysql_num_row is counting table row $count=mysql_num_rows($result); //if result match $myusername and mypassword table row must be 1 row if($count==1){ //session_register("myusername"); $_SESSION['myusername'] = $myusername; header("location:index.php"); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?> The code in the header file. [Appears in the Application Information Section] <?php if(isset($_SESSION['myusername'])) { echo "Welcome, ". $_SESSION['myusername']; } else { echo "Welcome, Guest"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432129 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 Can you also post index.php, since this is the place you are redirected after a successful login. Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432133 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 <?php session_start(); ob_start(); correct <?php ob_start(); session_start(); and the header file needs session_start() you been playing with php.ini Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432137 Share on other sites More sharing options...
smithmr8 Posted January 6, 2008 Author Share Posted January 6, 2008 I hadn't put 'session_start()' in the header file. Welcome, Luke Its working now . The last thing, was how do I use that name to get more values from the 'members' table for that username. I have tried to do things like .. <?php $user = $_SESSION['username']; $info="SELECT * FROM members WHERE username='$user'"; $result_info = mysql_query($info); ?> Which, when using this line, displays nothing. echo "ID: ". $result_info['ID']; Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432146 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 You're missing a line. You need to grap the results first, using one of the mysql_fetch_xxxx functions: <?php $user = $_SESSION['username']; $info="SELECT * FROM members WHERE username='$user'"; $result_info = mysql_query($info) or die(mysql_error()); $row = mysql_fetch_assoc($result_info); echo "ID: ". $row['ID'];//this assumes you have a field called ID ?> Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432149 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 <?php $user = $_SESSION['username']; $info="SELECT * FROM members WHERE username='$user'"; $result_info = mysql_query($info); $x=mysql_fetch_array($result_info); echo "".$x['name'].""; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432150 Share on other sites More sharing options...
smithmr8 Posted January 6, 2008 Author Share Posted January 6, 2008 Thank-You so much! Its working now Very Much Appreciated. You guys definatelly know your stuff Best Regards, Luke PS. PHPFreaks will definately have a link from my site . Quote Link to comment https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/#findComment-432155 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.