PHP MYSQL:如何保持变量活动直到连接结束

发布于 2024-11-09 08:26:03 字数 540 浏览 0 评论 0原文

我想保持一些变量处于活动状态,以便网站的所有页面都可以使用它; 我尝试了全局,但这不适用于此类问题; 我使用以下代码:

 while($result1 = mysql_fetch_array( $result)) 
  {   
  $adm_no = $result1['adm_no']; 
  $adm_dt = $result1['adm_dt']; 
  $name = $result1['name']; 
  $dob = $result1['dob']; 
  $f_name = $result1['f_name']; 
  $f_office = $result1['f_office']; 
  $f_o_no = $result1['f_o_no']; 
  $m_name = $result1['m_name']; 
  $m_office = $result1['m_office']; 
  $addr = $result1['addr']; 
  $pho_no = $result['pho_no'];

另一个名为 tc.php 的页面中的相同变量。我该怎么办?

i want to keep some variable alive so that it is available to all the pages of the site ;
i tried global but that don't work with these kind of problem ;
i use the following code :

 while($result1 = mysql_fetch_array( $result)) 
  {   
  $adm_no = $result1['adm_no']; 
  $adm_dt = $result1['adm_dt']; 
  $name = $result1['name']; 
  $dob = $result1['dob']; 
  $f_name = $result1['f_name']; 
  $f_office = $result1['f_office']; 
  $f_o_no = $result1['f_o_no']; 
  $m_name = $result1['m_name']; 
  $m_office = $result1['m_office']; 
  $addr = $result1['addr']; 
  $pho_no = $result['pho_no'];

these same variable in another page called tc.php . how can i do that ????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杀手六號 2024-11-16 08:26:03

如果您想在另一个页面中再次访问所有数据,我建议您存储在会话中从 mysql 表检索数据所需的信息,而不是查询结果。这意味着您的会话空间中没有大量琐碎的数据。例如。

想象一下,我有一个人员表,想要在不同页面上获取该人员的信息,我只需将 person_id 存储在会话中,如下所示:

//home.php
$_SESSION['personID'] = $personID;

然后在任何我想要检索人员信息的页面上,我只需从会话中获取人员 id并运行查询以获取我需要的特定信息。

//profile.php
$personID = $_SESSION['personID'];

//Get specific information here

如果你真的无法改变你这样做的方式,我真的希望你可以,因为它会让你的生活变得更加轻松,那么只需将你的代码更改为:

//make sure that you have started a session at the top of your page before you do anything else
session_start();

while($result1 = mysql_fetch_array($result)) {   
    $_SESSION['adm_no'] = $result1['adm_no']; 
    $_SESSION['adm_dt'] = $result1['adm_dt']; 
    $_SESSION['name'] = $result1['name']; 
    $_SESSION['dob'] = $result1['dob']; 
    //etc
}

If you want to access all that data again in another page I would recommend storing the information needed to retrieve data from your mysql table in a session rather than the result of the query. This means you don't have a load of trivial data in your session space. For example.

Imagine I have a person table and want to get bits of information for that person on different pages I just store the person_id in a session like so:

//home.php
$_SESSION['personID'] = $personID;

Then on any page I want to retrieve person information on I just get the person id from the session and run the query to get the specific information I need.

//profile.php
$personID = $_SESSION['personID'];

//Get specific information here

If you really cant change the way that you are doing this which I really hope you can as it'll make your life a hell of a lot easier then just changing your code to this:

//make sure that you have started a session at the top of your page before you do anything else
session_start();

while($result1 = mysql_fetch_array($result)) {   
    $_SESSION['adm_no'] = $result1['adm_no']; 
    $_SESSION['adm_dt'] = $result1['adm_dt']; 
    $_SESSION['name'] = $result1['name']; 
    $_SESSION['dob'] = $result1['dob']; 
    //etc
}
茶花眉 2024-11-16 08:26:03

使用

$_SESSION['myvar']= "your value";

echo $_SESSION['myvar']; 

will可以访问任意页面

Use

$_SESSION['myvar']= "your value";

echo $_SESSION['myvar']; 

will can access any page

萌逼全场 2024-11-16 08:26:03

在 tc.php 中再次获取数据 - 我认为这是这种情况下最好的方法。

您还可以将该数据设置到会话中,并在 tc.php 中从那里获取它。

Fetch data again in tc.php - it is the best way in this case I think.

You can also set that data to the session, and in tc.php get it from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文