0

I'm busy with a voting script but, I've a problem when I want to vote something, I receive an error:

Fatal error: Call to a member function query() on a non-object in on Line 7

<?php
include '../config.php';
function getAllVotes($id)
{
    $votes = array();
    if($q_13 = "SELECT * FROM post WHERE id = $id");
    if($r_26 = $db->query($q_13));
?>

And this is my config.php

<?php

$db = new mysqli('localhost', 'root', 'mysql', 'me');

if($db->connect_errno) {
    die('Sorry, we are having some problems.');
}

?>

  • http://www.php.net/manual/en/language.variables.scope.php – hakre Apr 20 '14 at 14:38
  • 1
    possible duplicate of [Should I pass my $mysqli variable to each function?](http://stackoverflow.com/questions/14016462/should-i-pass-my-mysqli-variable-to-each-function) – hakre Apr 20 '14 at 14:40

1 Answers1

0

This is a scope issue , you need to pass the $db to your getAllVotes() as a parameter.

Like this...

function getAllVotes(mysqli $db, $id) //<---- Pass $db here
{
hakre
  • 193,403
  • 52
  • 435
  • 836
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • @hakre Thank you, Also I've this function: `function getEffectiveVotes(mysqli $db,$id)` and receiving this error: `Parse error: syntax error, unexpected T_VARIABLE` and this is the line: `$effectiveVote = getEffectiveVotes(mysqli $db,$id);` – user3490572 Apr 20 '14 at 15:13
  • When you call the function, only pass the variable, do not add `mysqli` in front. It's only for the function definition, not for function calling - `$effectiveVote = getEffectiveVotes($db, $id);`. See: http://www.php.net/manual/en/language.oop5.typehinting.php – hakre Apr 20 '14 at 15:15
  • @hakre thank you it's working! this was something new for me thanks for learning me something new! have a nice day – user3490572 Apr 20 '14 at 15:21
  • You're welcome. Learn the tools and use them wisely. If you don't understand a detail, locate it in the PHP manual. It's most often insightful (and if you can't locate it, ask about it). – hakre Apr 20 '14 at 15:22