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.)

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

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法