0

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

7
  • 6
    Hello bobby tables Commented Jul 12, 2011 at 13:31
  • 1
    looking at your code, $r is out of scope where you refer to your problem. place that echo inside the while loop Commented Jul 12, 2011 at 13:32
  • 4
    ARGHSQLINJECTION why are people still failing at this in 2011?! Commented Jul 12, 2011 at 13:32
  • 1
    @Tomalak Geret'kal — Probably (at least partially) due to W3Schools (spit) continuing to teach PHP/SQL without mentioning SQL injection once. Commented Jul 12, 2011 at 13:45
  • 1
    @Quentin: Go go class action lawsuit Commented Jul 12, 2011 at 14:53

4 Answers 4

2

Line 8 of your code

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

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

No, they have values. PHP has no block scope. That's not to say that this is correct, but it cannot cause "no values".
1

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

1 Comment

No, PHP has no block scope. That's not to say that this is correct, but it cannot cause "no values".
0

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']; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.