beach bum Posted May 7, 2010 Share Posted May 7, 2010 hello. i am having a problem i think has to do with $_SESSION. when i load the member.php page after the login page, a mysql query is run and returns the users 'friends' and displays then in a list as well as the total number of friends. this is working perfectly. but when i leave this page and come back to it, it says the user has 0 friends. i cannot figure out why the information is either lost, or why the query does not run again to select the friends unless they enter the member.php page from the login page. while on the member.php page if you click the 'home' link, it points to the member.php page, but does not retain the correct query results. following is my member.php page (which mostly calls other functions in external files) as well as the query function that selects the friends. any help you can give to set me in the right direction is appreciated. i am pretty new to php and got most of this code from the book 'PHP and MySQL Web Development' from Developer's Library and am trying to make it work for my personal needs. --------------------- member.php -------------------- <?php // include function files for this application require_once('login_fns.php'); session_start(); //create short variable names $username = $_POST['username']; $passwd = $_POST['passwd']; if ($username && $passwd) { // they have just tried logging in try { login($username, $passwd); // if they are in the database register the user id $_SESSION['valid_user'] = $username; } catch(Exception $e) { // unsuccessful login do_html_header('Problem:'); echo 'You could not be logged in. You must be logged in to view this page.'; do_html_url('login.php', 'Login'); do_html_footer(); exit; } } do_html_header('Home'); check_valid_user(); get_friends($username); display_user_menu(); do_html_footer(); ?> ---------------------- get friends function ---------------------- function get_friends($username) { $conn = db_connect(); $result = $conn->query("select friendname FROM friends WHERE username='".$username."'"); $friends = $result; $num_results = $result->num_rows; if ($num_results == 0) { echo "</br>"; echo "You do not have any friends. Loser."; } else { echo "<p>Number of friends: ".$num_results."</p>"; for ($i=0; $i < $num_results; $i++) { $row = $friends->fetch_assoc(); echo "<br />"; echo $row['friendname']; } } } Quote Link to comment https://forums.phpfreaks.com/topic/201052-losing-query-results-when-page-reloads/ Share on other sites More sharing options...
luca200 Posted May 11, 2010 Share Posted May 11, 2010 First of all, this is not a MySql issue, so you are in the wrong forum. Anyway, your problem is pretty simple. You call get_friends() with an argument filled by a POST field. So if the user hasn't just logged in, that argument is empty. Quote Link to comment https://forums.phpfreaks.com/topic/201052-losing-query-results-when-page-reloads/#findComment-1056420 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.