psycoperl Posted June 26, 2018 Share Posted June 26, 2018 I am receiving the following error in my PHP code - Fatal error: Call to a member function query() on null in /home3/my_user/public_html/wine.php on line 177 Line 177 of my code is: $result_NWP = $conn->query($sql_NWP) or die($conn->error); Here is the segment of my php code, starting from line 156 and ending on line 181. <?PHP $sql_WP = "SELECT * FROM GH_WinePurchaseList_VW1"; $result_WP = $conn->query($sql_WP) or die($conn->error); $sql_WPStat = "SELECT count(PurchaseID) as NumWinesPurchased, Sum(PurchaseQuantity) as BottlesPurchased FROM tblWinePurchased"; $result_WPStat = $conn->query($sql_WPStat) or die($conn->error); $row_WPStat = $result_WPStat->fetch_assoc(); $sql_WYRMin = "Select MIN(EXTRACT(YEAR from B.VisitDate)) as MinYear From tblVisits B"; $result_WYRMin = $conn->query($sql_WYRMin) or die($conn->error); $row_WYRMin = $result_WYRMin->fetch_assoc(); $sql_WYRMax = "Select MAX(EXTRACT(YEAR from B.VisitDate)) as MaxYear From tblVisits B"; $result_WYRMax = $conn->query($sql_WYRMax) or die($conn->error); $row_WYRMax = $result_WYRMax->fetch_assoc(); function fWineStats($W_YR){ $sql_NWP = "Select Count(A.PurchaseID) as NWP, Sum(A.PurchaseQuantity) as NBP From tblWinePurchased A Inner Join tblVisits B on A.PurchaseVisit = B.VisitID WHERE EXTRACT(YEAR from B.VisitDate) ='" . $W_YR . "' Group By EXTRACT(YEAR from B.VisitDate)"; //var_dump($sql_NWP); $result_NWP = $conn->query($sql_NWP) or die($conn->error); $row_NWP = $result_NWP->fetch_assoc(); return array ($NWP, $NBP); } ?> I do not know what I can do to fix this? Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 26, 2018 Share Posted June 26, 2018 You have at least 2 problems in your code: You don't seem to be familiar with the basic rules of PHP variable scoping. You have a function fWineStats(). Inside that function you attempt to reference an object $conn and call a method $conn->query(). The problem is that you did not pass the $conn object into fWineStats() as a parameter, or declare $conn inside the function as a global variable. The simplest solution would be to change the function to pass in the parameter, but you will also need to update wherever fWineStats() is actually called. That is not shown in your code. Your return values are also messed up. You are returning an array with variables that don't exist in your function ($NWP, $NBP). Personally I don't see why you would be trying to return an array when really what you want to return is return a single row. I did my best to guess what you really wanted, and just returned the row fetched. function fWineStats($conn, $W_YR){ $sql_NWP = "Select Count(A.PurchaseID) as NWP, Sum(A.PurchaseQuantity) as NBP From tblWinePurchased A Inner Join tblVisits B on A.PurchaseVisit = B.VisitID WHERE EXTRACT(YEAR from B.VisitDate) ='" . $W_YR . "' Group By EXTRACT(YEAR from B.VisitDate)"; //var_dump($sql_NWP); $result_NWP = $conn->query($sql_NWP) or die($conn->error); $row_NWP = $result_NWP->fetch_assoc(); return $row_NWP; } Quote Link to comment 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.