会话以回显从数据库检索的行

时间:2014-03-08 15:56:24

标签: php mysql session

我有一个用户可以发送和接收消息的系统,我需要让他们直接回复消息。

这是一个测试代码,用于使页面回显应该检索并设置为会话的内容,但它不会回显任何内容。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Clan Kaos ● View Post</title>
  <link rel="stylesheet" type="text/css" href="include/style/content.css" />
</head>
<body>

<?php

include ("include/header.html");

include ("include/sidebar.html");

include ("include/testpost.html");
?>
<div class="container">

<?php   session_start();
  require_once('appvars.php');
  require_once('connectvars.php');

  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

      $query = "SELECT username FROM kaoscraft_user WHERE user_id = '" . $_SESSION['user_id'] . "'";
        $data = mysqli_query($dbc, $query);
            $row = mysqli_fetch_array($data);
            $username1 = $row['username'];

  // Grab the profile data from the database
  $query = "SELECT `to`, `from`, rank, gender, picture, title, msg FROM kaoscraft_pm WHERE `to` = '$username1' ORDER BY msg_id DESC";
  $data = mysqli_query($dbc, $query);
  $gender = $row['gender'];
    $username2 = $_SESSION['reply'];


  while ($row = mysqli_fetch_array($data)) {
    $_SESSION['reply'] = $row['from'];
  echo '<div class="viewpost">';
  echo '<div class="vpside">';
    if(!empty($row['picture'])) {
    echo '<img class="pictest" src="' . MM_UPLOADPATH . $row['picture'] . '" alt="' . MM_UPLOADPATH . 'nopic.png' . '" />';
    }
  if(!empty($row['from'])) {
      echo '<p>From:<br />' . $row['from'] . '</p>';
        echo '<p> ' . $username2 . '</p>';
    }
  if(!empty($row['rank'])) {
      echo '<p>Rank:<br />' . $row['rank'] . '</p>';
    }
  if(!empty($row['gender'])){
    echo '<p>Gender:<br /> ' . $row['gender'] . '</p>';
  }  
  echo '</div>';

    if(!empty($row['title'])) {
        echo'<h4><u>' .$row['title']. '</u></h4>';
    }
      if(!empty($row['msg'])) {
      echo '<p class="">' . $row['msg'] . '</p>';
    }
        echo '<div class="sig">';
      if(!empty($row['bio'])) {
      echo '<p>' . $row['bio'] . '</p>';
      }
      echo '</div>';
    echo '</div><br />'; 
    }
  mysqli_close($dbc); 
?>

我需要将会话$_SESSION['reply']设置为变量$username2,但它似乎不起作用。

2 个答案:

答案 0 :(得分:2)

在PHP代码之上移动session_start();。您之前访问的$_SESSION['reply'] = $username2;无效。

答案 1 :(得分:1)

我在做什么是不正确的。我不得不搬家

$_SESSION['reply'] = $row['from'];
$username2 = $_SESSION['reply'];`

从查询到

之后
while ($row = mysqli_fetch_array($data)) {
    $_SESSION['reply'] = $row['from'];
    $username2 = $_SESSION['reply'];`

然后它正确回声。感谢大家的帮助:))