【问题标题】:retrieving php session values to use as variables afterwards?之后检索 php 会话值以用作变量?
【发布时间】:2013-11-06 05:19:33
【问题描述】:

我在这里有一些可能非常简单的东西,但过去 30 分钟我在 Google 上搜索但没有成功,所以我希望有人能帮助我解决我做错的事情。

我在服务器上的会话中存储了某些值,我正在尝试按每个用户检索它们,然后在进一步的查询中使用。但是由于某种原因,它的行为就好像值是空的,所以我不知道我做错了什么?

$session_country = $_SESSION['my_country_code'];
$user_country = $db->get_sql_field("SELECT id FROM db_countries WHERE country_iso_code='".$session_country."'","country");
echo "your country is ";
echo $user_country;

我非常感谢任何关于我做错了什么的提示:(

当我这样做时:

<? print_r($_SESSION['my_country_code']); ?>

显示很好,没有问题!

//编辑

这里是 get_sql_field 的代码:

function get_sql_field ($query, $field, $null_message = NULL)
    {
            (string) $field_value = NULL;

            $query_result = $this->query($query);

            if ($this->num_rows($query_result))
            {
                    $field_value = $this->sql_result($query_result, 0, $field);
            }
            else
            {
                    $field_value = $null_message;
            }

            return $field_value;
    }

【问题讨论】:

  • sql明显有问题
  • 您是否在脚本顶部正确声明了session_start()
  • 粘贴 get_sql_field 的代码和有趣的 db_countries

标签: php mysql variables session


【解决方案1】:

在我看来,您实际上并没有在查询中选择“国家”字段(只是“id”),而是在尝试访问“国家”字段。要么选择“国家”字段,要么将 get_sql_field() 调用的第二个参数更改为“id”,如果那是你真正想要得到的。

【讨论】:

  • 哇,就是这样!我知道这是一件非常愚蠢的事情!非常感谢:)
  • 没问题。 :) 有时只需要第二双眼睛。
【解决方案2】:

试试这个:

$session_country = $_SESSION['my_country_code'];
$qry = "SELECT country FROM db_countries WHERE  country_iso_code='".$session_country."'";
$user_country = $db->get_sql_field($qry,"country");
echo "your country is ".$user_country;

【讨论】:

  • 您的建议与 OP 发布的内容有何不同(使其更引人注目)?另外,“试试这个”如何被视为答案?
  • 对不起,我只是发布我的答案;我在我的本地主机上测试这段代码,同时写下答案……我只是错过了上一个答案
猜你喜欢
  • 1970-01-01
  • 2011-05-11
  • 2011-06-05
  • 2011-12-27
  • 1970-01-01
  • 2013-11-10
  • 2015-09-30
  • 2012-08-17
  • 2014-05-16
相关资源
最近更新 更多