简体   繁体   中英

php echo not displaying

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

Line 8 of your code

<div class="message2"><?php echo "".$r['message'].""; }?></div> 

Why is that closing curly brace in there before the closing ?> ?

You seem to have closed the WHILE loop here:

<div class="message2"><?php echo "".$r['message'].""; }?></div>

Notice the curly bracket.

Therefore, votes_up and votes_down have no values.

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

This is because the $r['...'] variable is out of the scope where you place it.

Better way to do it (assuming you only fetch one row:

<?
   $sql = mysql_query("SELECT * FROM threads WHERE id = '" . $_GET['id'] . "'") or die (mysql_error());
   $row = mysql_fetch_assoc($sql);
?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM