dongxingchang9345 2012-12-30 03:05
浏览 39

无法修改标题信息 - 与MySQL查询相关[重复]

Possible Duplicate:
Headers already sent by PHP

I fixed this problem, but since I don't understand what caused it, I can't be sure it's really fixed.

My PHP site displays the latest activity on the home page, before you log in. I recently modified the logic to include more types of activity. It looked like it worked, but I got the following error upon logging in:

Warning: Cannot modify header information - headers already sent by
(output started at header.php:75)
in index.php on line 26

I think this error message is misleading, because the way I fixed it was by changing "LIMIT 10" to "LIMIT 9" in the MySQL query that gets the activity to be displayed on the home page.

    public function getLatestActivity()
{
    $sql = "SELECT 'Debate' AS Type, d.Debate_ID AS ID, CONCAT('debate.php?debate_id=', d.Debate_ID) AS URL, d.Title, d.Add_Date
        FROM debates d

        UNION SELECT 'Discussion' AS Type, d.Discussion_ID AS ID, CONCAT('discussion.php?discussion_id=', d.Discussion_ID) AS URL, d.Title, d.Add_Date
        FROM discussions d

        UNION SELECT 'Petition' AS Type, p.Petition_ID AS ID, CONCAT('petition.php?petition_id=', p.Petition_ID) AS URL, p.Petition_Title AS Title, p.Add_Date
        FROM petitions p

        ORDER BY Add_Date DESC
        LIMIT 9";

    try
    {
        $stmt = $this->_db->prepare($sql);
        $stmt->execute();
        $activity = array();
        while ($row = $stmt->fetch())
        {
            $activity[] = '<span style="font-size: x-large"><strong>Latest activity</strong></span><br /><span style="font-size: large">' . $row['Type'] . ': <span style="color: #900"><a href="' . $row['URL'] . '" style="color: #900">' . $row['Title'] . '</a></span></span>';
        }
        $stmt->closeCursor();

        return $activity;
    }
    catch(PDOException $e)
    {
        return FALSE;
    }
}

And here's what I'm doing with the data returned by that function. It loops through the array and shows a new item every 4 seconds.

    <?php $latest_activity = $pdb->getLatestActivity(); ?>
<script type="text/javascript">
    var activity = <?php echo json_encode($latest_activity);  ?>;
    var index = -1;
    $(function()
    {
        getLatestActivity();
    });
    function getLatestActivity()
    {
        index = (index + 1) % activity.length;
        var div = document.getElementById('divLatestActivity');
        if (index < activity.length)
        {
            div.innerHTML = activity[index];
        }
        setTimeout("getLatestActivity()", 4000);
    }
</script>

Why did changing "LIMIT 10" to "LIMIT 9" fix the "cannot modify header information" problem?

  • 写回答

2条回答 默认 最新

  • double0201 2012-12-30 03:11
    关注

    In PHP, if you use the header() function (i.e. header("Location:login.php"); to redirect to the login.php page), you must do it before any other code that might output text to the browser.

    //this will CAUSE a warning
    echo "Login now";
    session_start()
    header("content-type:text/html");
    header("cache-control:max-age=3600");
    

    However...

    //this will NOT CAUSES a warning
    header("content-type:text/html");
    header("cache-control:max-age=3600");
    session_start();
    echo "Login now";
    

    So comb through your code that executes any session_start() or header() directives and make sure there are no echo ""; before them. If you have any MySQL warnings that are thrown out onto the page before your session_start() or header() that will also cause this warning.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?