0

why is my function returning undefine variable pdo? i have tried all i could to get it to work but to no avail. even when i put the database connection in same page, still wont work. what could be the problem thanks

            function referralCount($uid,$reflvl)
            {

            $stmt= $pdo->query("SELECT * FROM scraffiliateusr WHERE usrinvby='$uid'");
            $nusrref1 = $stmt->rowCount();
            //$arrusrref1 = $stmt->fetch(PDO::FETCH_LAZY);

            $reflvl1=$nusrref1;
            $ttlreflvl2="0";
            $ttlreflvl3="0";

            for ($i=0; $i<$nusrref1; $i++)
            {
            $arrusrref1 = $stmt->fetch(PDO::FETCH_LAZY);
                $stmt= $pdo->query("SELECT * FROM scraffiliateusr WHERE usrinvby='$arrusrref1[0]'");
                $nusrref2 = $stmt->rowCount();
                //$arrusrref2 = $stmt->fetch(PDO::FETCH_LAZY);
                $ttlreflvl2=$ttlreflvl2+$nusrref2;


                    for ($j=0; $j<$nusrref2; $j++)
                    {
                    $arrusrref2 = $stmt->fetch(PDO::FETCH_LAZY);

                    $stmt= $pdo->query("SELECT * FROM scraffiliateusr WHERE usrinvby='$arrusrref2[0]'");
                    $nusrref3 = $stmt->rowCount();
                    //$arrusrref3 = $stmt->fetch(PDO::FETCH_LAZY);

                    $ttlreflvl3=$ttlreflvl3+$nusrref3;
                    }
            }
            $reflvl2=$ttlreflvl2;
            $reflvl3=$ttlreflvl3;
            if($reflvl=='1')
            {
            return($reflvl1);
            }
            elseif($reflvl=='2')
            {
            return($reflvl2);
            }
            elseif($reflvl=='3')
            {
            return($reflvl3);
            }

            }
Chidi
  • 35
  • 4
  • @meagar all you do is mark a question as either duplicate or something but avoid answering questions you might not have an answer to and hinder others that do the pleasure of helping someone out. you coold at least place a link to where the question have been answered before and it might be two different questions altogether! – Chidi Dec 21 '14 at 13:17
  • That's how this site works. Your question has been asked and answered many times before. – user229044 Dec 21 '14 at 16:24
  • is there truely, really a question in this world that haven't been asked before? – Chidi Dec 22 '14 at 05:15
  • Thanks for the reproach Meagar, at least it thought me something too. – Chidi Dec 22 '14 at 05:44
  • That was not a reproach. I'm trying to tell you how to use this site. Your question was a duplicate of an existing question. If you'd read that question, you would have found your answer. – user229044 Dec 22 '14 at 05:57

1 Answers1

0

I believe you're running into a problem of variable scope.

See here: http://php.net/manual/en/language.variables.scope.php

Meetarp
  • 2,331
  • 1
  • 23
  • 31
  • Thanks. what i did was place my connection variables inside the function. but i have many functions, so why do i have to do i new connection in every function? why not one connection to work for all the functions? – Chidi Dec 22 '14 at 05:30
  • but on one of the function i'm having this error: Catchable fatal error: Object of class PDORow could not be converted to string – Chidi Dec 22 '14 at 05:31
  • I've fixed the issue. Thanks Mephoros – Chidi Dec 22 '14 at 05:42