Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/149471-external-variable/
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/149471-external-variable/#findComment-784986
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/149471-external-variable/#findComment-784991
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/149471-external-variable/#findComment-784997
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.