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 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路