dragon012100 2011-07-12 13:29
浏览 30
已采纳

php echo不显示

This code

<?php  echo "Likes: ".$r['votes_up']."&nbsp;"; echo "Dislike: ".$r['votes_down'].""; ?>        

Wont post the values from the table for 'votes_up' 'votes_down'
I cant get my head round this! Ive got this exact code working on a different page but it wont on this.
Heres the entire code ....

    <div class="message">
<?php 
$sql = mysql_query("SELECT * FROM threads WHERE id = '" . $_GET['id'] . "'") or die(mysql_error());     
    while($r = mysql_fetch_array($sql))  {
$posted = date("jS M Y h:i",$r['posted']); echo "".$r['author']." &nbsp; $posted";?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo "".$r['message'].""; ?>">
        Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2"><?php echo "".$r['message'].""; }?></div> 
<?php  echo "Likes: ".$r['votes_up']."&nbsp;"; echo "Dislike: ".$r['votes_down'].""; ?>        
</div>
<br/>
<hr>

Can anyone help? its driving me insane

  • 写回答

4条回答 默认 最新

  • dougou6727 2011-07-12 13:32
    关注

    Your curly bracket is being placed before the last echo statement, therefore the $r variable is out of scope.

    Move the } to later in your page like so

    <div class="message">
    <?php 
        $sql = mysql_query("SELECT * FROM threads WHERE id = '" . mysql_real_escape_string($_GET['id']) . "'") or die(mysql_error());     
        while($r = mysql_fetch_array($sql))
        {
            $posted = date("jS M Y h:i",$r['posted']);
            echo $r['author']." ".$posted;
    ?>
    <a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $r['message']; ?>">
        Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
    <div class="message2">
        <?php
            echo $r['message']; 
        ?>
    </div> 
        <?php
            echo "Likes: ".$r['votes_up']."&nbsp;";
            echo "Dislike: ".$r['votes_down'];
        } 
        ?>        
    </div>
    <br/>
    <hr>
    

    Also notice the call to mysql_real_Escape_string in the $sql var. this will prevent nasty sql injections

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效