rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 How do you set $_SESSION['id'] as you mentioned.??? Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215393 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 Okay I tried this codes and got a Welcome no name but it goes to the next private pages. so I think it somehow moved from the everyone in the db to welcoming no one but I think it must be the layout of the echo Welcome, firstname. Here is my new codes $query = "SELECT id, firstname FROM `Members` WHERE id='" . $_SESSION['id'] . "'"; $result = mysql_query($query); if($result){ // or do it the way you have if you prefer "(!$result)" $row=mysql_fetch_assoc($result); echo "Welcome, {$row['firstname']}"; }else{ $message = 'Invalid query:' . mysql_error() . "\n"; $message .= 'Whole query:' . $query; die($message); } } //Free the resources associated with the result set mysql_free_result($result); this is my results live Welcome, Your new Member accounts lets you enter the members only section of our web site. You'll find special discounts, a profile of matches, live advise from experts, and much more. Your new Member ID and password were emailed to you. Store them carefully for future use. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215399 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 How do you set $_SESSION['id'] as you mentioned.??? $_SESSION['id'] = $row['id']; but in order for it to work properly you'll have to put that snippet BEFORE the query, yet inside the while loop (or else $row won't exist). In other words, you should have $_SESSION['id'] set before you even run a query with it. // Set the users session ID include("Connections/connect_to_mysql.php"); This is where the session should be set... you probably have something wrong in connect_to_mysql.php Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215407 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 I have the same connect to db as the form which is uploading the tested names. I am now getting a welcome, " I just need to work on the echo correctly. thanks for all your help I so appreciate it. here is my codes for the echo $row=mysql_fetch_assoc($result); echo "Welcome, " . $row['firstname'] . ""; Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215411 Share on other sites More sharing options...
rdkd1970 Posted May 15, 2011 Author Share Posted May 15, 2011 Still not getting any results with my if statements I am trying to echo out welcome, firstname but when I adjust my if statements it either welcomes all names in db, just welcome or nothing but it does continue with the private pages. Here is my latest codes <?php session_start(); ini_set ("display_errors", "1"); error_reporting(E_ALL); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome</title> <style type="text/css"> .background {color: #B56AFF; } </style> </head> <body> <p> <?php /* Program: login.php * Desc: Displays the new member welcome page. Greets * member by name and gives a choice to enter * restricted section or go back to main page. */ if (isset($_SESSION['id'])) { // Set the users session ID $id=$_SESSION['id']; include_once ("Connections/connect_to_mysql.php"); //Formulate Query //This is the best way to perform an SQL query $query = "SELECT id, firstname FROM `Members` WHERE id={$_SESSION['$id']}"; $result = mysql_query($query); $numrows=mysql_num_rows($result); //Check result //This shows the actual query sent to MySQL and the error. Useful for debugging. if(!$result){ $message = 'Invalid query:' . mysql_error() . "\n"; $message .= 'Whole query:' . $query; die($message);} //Use result //Attempting to print $result won't allow access to information in the resource //One of the mysql result functions must be used //See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. if($id==$firstname){ echo "Welcome, '{$row['firstname']}'"; } //Free the resources associated with the result set mysql_free_result($result); } ?> </p> <p> </p> <p>Your new Member accounts lets you enter the members only section of our web site. You'll find special discounts, a profile of matches, live advise from experts, and much more.</p> <p>Your new Member ID and password were emailed to you. Store them carefully for future use.</p> <div style="text-align: center"> <p style="margin-top: .5in; font-weight: bold"> Glad you could join us!</p> <form action="profile.php" method="post"> <input type="submit" value="Enter the Members Only Section"> </form> <form action="index.php" method="post"> <input type="submit" value="Go to Main Page"> </form> </div> </body> </html> At a lost :'( Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215607 Share on other sites More sharing options...
Zane Posted May 15, 2011 Share Posted May 15, 2011 Ok, I just decided to restructure your code rather than tell you everything wrong with it, I put a few comments in there to explain a few things. session_start(); ini_set ("display_errors", "1"); error_reporting(E_ALL); ?> Welcome <br /> .background {color: #B56AFF;<br /> }<br /> if (isset($_SESSION['id'])) { // Set the users session ID include_once ("Connections/connect_to_mysql.php"); $id=$_SESSION['id']; //Formulate Query //This is the best way to perform an SQL query $query = "SELECT id, firstname FROM `Members` WHERE id='$id'"; $result = mysql_query($query); $numrows = mysql_num_rows($result); //Check result //This shows the actual query sent to MySQL and the error. Useful for debugging. if(!$result){ $message = 'Invalid query:' . mysql_error() . "\n"; $message .= 'Whole query:' . $query; die($message); } /// Since the script die()'s if the query fails, you don't need an else statement. /// We can assume from here on out that the query passed and the SESSION id was set. $row = mysql_fetch_assoc($result); mysql_free_result($result); echo " Welcome, " . $row['firstname'] . ""; } ?> Your new Member accounts lets you enter the members only section of our web site. You'll find special discounts, a profile of matches, live advise from experts, and much more. Your new Member ID and password were emailed to you. Store them carefully for future use. Glad you could join us! Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215671 Share on other sites More sharing options...
rdkd1970 Posted May 15, 2011 Author Share Posted May 15, 2011 I tried the codes it returns an empty result. I also put the select into the MySQL SQL it also returned result empty. Should I use the * .?? Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215675 Share on other sites More sharing options...
Zane Posted May 15, 2011 Share Posted May 15, 2011 alright then, so if the Query is returning empty, it most likely means either: - the id doesn't exist - or the id hasn't been set. $_SESSION['id'] has no value. Which makes me wonder how it gets past that first IF statement in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215680 Share on other sites More sharing options...
rdkd1970 Posted May 15, 2011 Author Share Posted May 15, 2011 Could it mean the db query is dying and how could that be if it is the same as the form which adds to the db. OH, I think if the codes are not right then the connection would die anyway.??? Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215688 Share on other sites More sharing options...
Zane Posted May 15, 2011 Share Posted May 15, 2011 if (isset($_SESSION['username'])) { You check for this variable but I don't see where you actually declared it. SELECT id, firstname FROM `Members` WHERE id=idLIMIT 1 There is no space between id and LIMIT. Also, the fact that you have id=id in your query answers this puzzle. Essentially, that query says Get all users whose id is equal to their id... which is all of them. So now I'm back to my second post on this thread.. or third I can't remember. You never set $_SESSION['id'] to contain an actual id. Once you do that, you should have a working query... you won't even need that LIMIT duct tape. $_SESSION['id'] has no value. Post your "empty result" query here. You should notice that there is no id being compared. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215690 Share on other sites More sharing options...
rdkd1970 Posted May 15, 2011 Author Share Posted May 15, 2011 I just put this SQL into MySQL and got the names in the db all of them SELECT * FROM `Members` WHERE id=id (gets all users) WHERE id='id' (gets an empty result) WHERE id='$id' (gets an empty result) Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215691 Share on other sites More sharing options...
Zane Posted May 15, 2011 Share Posted May 15, 2011 You want your query to say something like SELECT * FROM `Members` WHERE id='45' or something with a number. You may not need to single quotes either. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215693 Share on other sites More sharing options...
rdkd1970 Posted May 15, 2011 Author Share Posted May 15, 2011 That would get a single row with the person in MySQL. I tried it live and actually got a name but it was the name before the one I just added. Yea almost there!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1215718 Share on other sites More sharing options...
rdkd1970 Posted May 16, 2011 Author Share Posted May 16, 2011 I tried the DATETIME, NOW() thought that would work with the number but no luck. Has anyone tried the $id|id I am going to try that do you think it is possible. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1216191 Share on other sites More sharing options...
rdkd1970 Posted May 18, 2011 Author Share Posted May 18, 2011 I got it. After the registration page, I created a page that had welcome for the member to click on a login button which takes them to the login page. After they login in it goes to the first private page which echos their name. At the top of the welcome, and member is the session_start with the $_SESSION[username] coding. Which goes on all pages that are private. 1. Form.php to 2. Register_success to 3. Login_form to 4. Login_users to 5. Access_denied (if $_SESSION) does not recognize the login member 6. Member (if the login worked as the member registered) 7. Login_failed if no username or password. If anyone wants these scripts let me know.... Thanks to everyone that helped me in getting it done. :D Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/page/2/#findComment-1217189 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.