duanqian6982 2019-07-19 06:21
浏览 48
已采纳

回显时不显示会话信息/数据

I'm trying to echo out the information in the database profile in a session called $_SESSION['about'], but whenever I echo it out nothing shows. I want it to show the information in the about_me table.

I inserted the information into my database and tried setting it to a session and using it on other pages to no avail.

if(isset($_POST['aboutme-submit'])){

    require 'profiles.dbh.inc.php';

$about = mysqli_real_escape_string($conn2 ,$_POST['aboutme']);

$sql3 = "INSERT INTO profile(about_me ) VALUEs('$about')";
mysqli_query($conn2, $sql3);

$query = "SELECT * FROM profile WHERE about_me;"; 
$result = mysqli_query($conn2, $query); 
$result_check = mysqli_num_rows($result);
if ($result_check > 0){
    while ($row = mysqli_fetch_assoc($result)) {
   }
}
session_start();
$_SESSION['about'] = $row['about_me'];
header("Location: ../profiles.php?nice.")

I expect the output of the information in the about_me row to be shown when I use $_SESSION['about'], but the actual output is nothing at all.

  • 写回答

1条回答 默认 最新

  • dongshuzhuo5659 2019-07-19 06:46
    关注
    if ($result_check > 0){
        while ($row = mysqli_fetch_assoc($result)) {
       }
    }
    

    The way such a loop over a database result set works, is that it loops as long as $row contains a non-empty data structure - and after the last record was processed, the fetch function will return NULL on the next call, which makes the loop end at that point.

     $_SESSION['about'] = $row['about_me'];
    

    Because you did this after the loop, $row doesn’t contain any data at this point, it is NULL - and accessing a property about_me “of” NULL of course doesn’t return anything.

    (Please go enable proper PHP error reporting first of all now - then PHP has a chance to tell you about such errors. See f.e. How do I get PHP errors to display? )

    So your problem here is not the session itself, but that the moment you are trying to write your data into it - you don’t actually have your data any more. You need to work with $row inside of the loop, not afterwards, when it has been overwritten with NULL.

    (If you only expect one record to be returned, you don’t need to loop though. But if you do expect multiple records and loop over them, then you might want to add them to an array in the session, otherwise only the last value will survive, if you overwrite the session entry every time.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置