douxian3170 2018-05-31 19:55
浏览 48
已采纳

会话变量不与嵌入式PHP共享

I have an hmtl file that I have named with a .php extension so I could access php session variable...

<?php
session_start();
$test_user_id = $_SESSION['my_user_id'];
?>

<!DOCTYPE html>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet'>
<style>
body { background-color: black; font-family: 'Montserrat';font-size: 15px; font-weight: Bold; direction: rtl}
.bar:hover { fill: brown; }
.axis--x path { display: none; }
.NOTlabel {font-family:'Montserrat'; font-size: 15px; font-weight: Bold}   
div.tooltip {position: absolute;text-align: center;width: 75px;height: 28px;padding: 2px;font: 12px sans-serif;background: lightsteelblue;border: 0px;border-radius: 8px;pointer-events: none;}    
text {direction: ltr;}    
</style>
<body>

<?php echo $test_user_id; ?>  

<script src="https://d3js.org/d3.v4.min.js"></script>
<script type="text/javascript" src="charts_v0.12.php"></script>

... and as you can see toward the end of the above code the following embedded php...

<?php echo $test_user_id; ?>  

to test that variable works and it does.

However, you will also see the following last line of the above code...

<script type="text/javascript" src="charts_v0.12.php"></script>

...which has the following code...

var data = [
    <?php

//session_start();
//$test_user_id = $_SESSION['my_user_id'];



//date_default_timezone_set('America/New_York');   
$dbconn = pg_connect("host=localhost dbname=postgres user=postgres password=kevin234")
    or die('Could not connect: ' . pg_last_error());


$query = pg_query_params($dbconn, 'select time::date, mode() WITHIN GROUP (ORDER BY watts) AS modal_value, date_part(\'week\', time), count(secs) / 60, round(avg(bpm) ), round(avg(cadence)), round(mode() WITHIN GROUP (ORDER BY watts) / round(avg(bpm)), 2) as ratio from "Workout" where work_interval_flag = 1 and user_id = $1 group by time::date, date_part(\'week\', time) having round(avg(watts)) > 0 order by time::date', array(2));

//$query = pg_query_params($dbconn, 'select time::date, mode() WITHIN GROUP (ORDER BY watts) AS modal_value, date_part(\'week\', time), count(secs) / 60, round(avg(bpm) ), round(avg(cadence)), round(mode() WITHIN GROUP (ORDER BY watts) / round(avg(bpm)), 2) as ratio from "Workout" where work_interval_flag = 1 and user_id = $1 group by time::date, date_part(\'week\', time) having round(avg(watts)) > 0 order by time::date', array($test_user_id));

.
.
.

... which works, but the last line which is commented out does NOT work, presumably because there is a problem with $test_user_id which is the only difference between the last two lines.

Ideas?

  • 写回答

1条回答 默认 最新

  • douhuo0884 2018-05-31 20:15
    关注

    Not sure what you want in the var data = [ (I assume you are fetching and echo rows data somewhere) but you can't output before the session_start():

    <?php
    
    session_start();
    $test_user_id = $_SESSION['my_user_id'];
    
    $dbconn = pg_connect("host=localhost dbname=postgres user=postgres password=kevin234")
        or die('Could not connect: ' . pg_last_error());
    
    $query = pg_query_params($dbconn, 'select time::date, mode() WITHIN GROUP (ORDER BY watts) AS modal_value, date_part(\'week\', time), count(secs) / 60, round(avg(bpm) ), round(avg(cadence)), round(mode() WITHIN GROUP (ORDER BY watts) / round(avg(bpm)), 2) as ratio from "Workout" where work_interval_flag = 1 and user_id = $1 group by time::date, date_part(\'week\', time) having round(avg(watts)) > 0 order by time::date', array($test_user_id));
    
    ?>
    
    var data = [
    
    <?php
    //fetch and echo row data?
    ?>
    
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加