0

I have got a small php function that does not return any value that I can see and I seek your assistance

function getAllVotes($id) {
    $votes = array();
    $q = "SELECT * FROM $tbl_name WHERE id = $id";
    $r = mysql_query($q);
    if (mysql_num_rows($r)) {
        $row      = mysql_fetch_assoc($r);
        $votes[0] = $row['vvotes_up'];
        $votes[1] = $row['vvotes_down'];
    }
    return $votes;
}


// $id = $_POST['id'];


$id = 3;
$cur_votes = getAllVotes($id);
echo $cur_votes[1];

?>

now the

echo $cur_votes[1];

does not print any thing on my screen how ever the $cur_votes actually is an array and I can confirm that by

echo $cur_votes;

Which returns " Array "

I don't understand why $cur_votes[1]; does not return me any thing. Could you please help me ? thanks.

14
  • 1
    var_dump($cur_votes);? Commented Mar 9, 2014 at 5:52
  • 1
    Try echo $cur_votes[0]; Commented Mar 9, 2014 at 5:52
  • $cur_votes[0] is also without any output. Commented Mar 9, 2014 at 5:54
  • Result to var_dump is array(0) { } Commented Mar 9, 2014 at 5:55
  • 1
    Where is $tbl_name defined? Commented Mar 9, 2014 at 5:57

1 Answer 1

1

make the following changes to your query

     $tbl_name="tablename"; 
     "SELECT * FROM '{$tbl_name}' WHERE id = '{$id}'";
Sign up to request clarification or add additional context in comments.

9 Comments

Integers don't need to be escaped.
Encouraging SQL injection... Downvoting wouldn't be enough.
Same result with both .. nothing is returned and the array is empty
can you check what your query returns?
Can you state the query ?
|

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.