I-AM-OBODO Posted December 21, 2014 Share Posted December 21, 2014 Hi guys. why is my function error: undefined varable pdo? 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/293209-undefined-variable-pdo/ Share on other sites More sharing options...
requinix Posted December 21, 2014 Share Posted December 21, 2014 You didn't define $pdo anywhere. And before you say "yes I did, it's outside the function", variables outside a function are not available inside the function - pass $pdo as another function argument. Quote Link to comment https://forums.phpfreaks.com/topic/293209-undefined-variable-pdo/#findComment-1500250 Share on other sites More sharing options...
Solution CroNiX Posted December 21, 2014 Solution Share Posted December 21, 2014 For more info on what requinix is saying, the problem (assuming you defined $pdo somewhere outside your function), is an issue of variable scope. Quote Link to comment https://forums.phpfreaks.com/topic/293209-undefined-variable-pdo/#findComment-1500293 Share on other sites More sharing options...
hansford Posted December 22, 2014 Share Posted December 22, 2014 You would need something along the lines of: $con = new PDO("$this->db:host=$this->host;dbname=$this->dbname",$this->user,$this->pass); If you wanted to use this in a function then you need to make the function aware of your database connection (variable scope) An easy way of accomplishing this is through dependency injection - simply pass the variable to your function in the form of an argument. function referralCount($uid,$reflvl,$DB) Quote Link to comment https://forums.phpfreaks.com/topic/293209-undefined-variable-pdo/#findComment-1500336 Share on other sites More sharing options...
I-AM-OBODO Posted December 23, 2014 Author Share Posted December 23, 2014 I finally know why it giving me the error. It's an issue of variable scope http://php.net/manual/en/language.variables.scope.php Quote Link to comment https://forums.phpfreaks.com/topic/293209-undefined-variable-pdo/#findComment-1500530 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.