zavineo Posted March 15, 2009 Share Posted March 15, 2009 I am attempting to create variables in one php file and recall the results from any other file. This is the code I am using in my variables file. $query = "SELECT * FROM users"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $member_id = $row['id']; $member_name= $row['user_name']; } This is the code I am using in other files to recall the data. include 'var.php'; echo $member_id; echo $member_name; It is not working the way I expected. this only works is I put both sets of code on the same page. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/149471-external-variable/ Share on other sites More sharing options...
.josh Posted March 15, 2009 Share Posted March 15, 2009 Data persistence. Variables do not persist from page to page unless you pass them somehow. You can -store/retrieve the data from a db on each page -store/retrieve the data from a flatfile on each page -store/retrieve the data from a a cookie -store/retrieve the data from a session variable -pass it to the next page via GET or POST method, using links, forms or header redirects Quote Link to comment https://forums.phpfreaks.com/topic/149471-external-variable/#findComment-784986 Share on other sites More sharing options...
zavineo Posted March 15, 2009 Author Share Posted March 15, 2009 The data that I am trying to retrieve is in a DB, but to retrieve for example a 'User Name' I have to insert the first block of code I mentioned on every page that needs the User Name. Quote Link to comment https://forums.phpfreaks.com/topic/149471-external-variable/#findComment-784990 Share on other sites More sharing options...
.josh Posted March 15, 2009 Share Posted March 15, 2009 So just pull it once, at the login page or whatever, and pass it to each page a different way. Depending on your needs, any one or more of those alternatives might work best for you. Just depends on your setup and goals. Session vars are the most common method; I suggest looking into that first. Quote Link to comment https://forums.phpfreaks.com/topic/149471-external-variable/#findComment-784991 Share on other sites More sharing options...
zavineo Posted March 15, 2009 Author Share Posted March 15, 2009 I may need to look into one of your other options. There is already a session started when the user logs in. I can retrieve that user data from the DB using that users session. I am assuming that only one session can be active at a time. The trouble is retrieve data about other users. Quote Link to comment https://forums.phpfreaks.com/topic/149471-external-variable/#findComment-784997 Share on other sites More sharing options...
.josh Posted March 15, 2009 Share Posted March 15, 2009 so...why can't you use a different session variable? You can have as many session variables as you want. Quote Link to comment https://forums.phpfreaks.com/topic/149471-external-variable/#findComment-785006 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.