jrm Posted August 21, 2008 Share Posted August 21, 2008 I am having trouble setting a variable with a ternary operation. I keep getting a "Notice: Undefined variable: StaffName in /var/www/html/StaffNameFunc.php on line 18" even though I have coded the variable to have a value. The line before works, but not this one.??? My Data: PNCID DTG AB AssistingStaff 143 2007-05-22 17:30 26 0 function StaffName($tStaffID) { include "Connection.php"; $staffquery = "Select StaffID, Last, left(First,1) as FI, left(Middle,1) as MI, RankID From Staff_be.Staff where Staffid = " . $tStaffID .";"; $staffresult = mysql_query($staffquery,$link) or die("\n<p>Could not run Staff Query: " . $staffquery . " : " . mysql_error()); while($staff = mysql_fetch_array($staffresult, MYSQL_ASSOC)) { $rankquery = "Select RankID,Rank from Staff_be.StaffRank where RankID =" . $staff["RankID"].";"; $rankresult = mysql_query($rankquery,$link) or die("\n<p>Could not run Rank Query: ". mysql_error()); while($rnk = mysql_fetch_array($rankresult,MYSQL_ASSOC)) { $Rank = $rnk["Rank"]; } $StaffName = $Rank . " " . $staff["Last"] . ", " . $staff["FI"] . " " . $staff["MI"]; // ***** line 18 ***** } return $StaffName; } function StaffPRTCount($tStaffID) { include "Connection.php"; //include "CadetNameFunc.php"; $PRTQuery = "Select PNCID, DTG, AB, AssistingStaff from PNC_be.PRT Where AB = " . $tStaffID ." or AssistingStaff = " . $tStaffID .";"; $PRTResults = mysql_query($PRTQuery, $link) or die("\n<p> Could not run PRTQuery : " . $PRTQuery ." : " . mysql_error()); if(mysql_num_rows($PRTResults)==0) { $PRT[] = array("PNCID" => 0, "DTG" => "N/A", "Title" =>"Unknown"); } else { while($PRTRow = mysql_fetch_array($PRTResults, MYSQL_ASSOC)) { $PNCQuery = "Select CadetID From PNC_be.PNC Where PNCID = " . $PRTRow["PNCID"] .";"; $PNCResults = mysql_query($PNCQuery, $link) or die("\n<p>Could not run PNCQuery : " . $PNCQuery ." : " . mysql_error()); if(mysql_num_rows($PNCResults)==0) { $CadetID = 107;} else { $PNCRow = mysql_fetch_array($PNCResults, MYSQL_ASSOC); $CadetID = $PNCRow["CadetID"]; } $CadetName = CadetName($CadetID); $AB = ($PRTRow["AB"]==0) ? "Unknown" : StaffName($PRTRow["AB"]); /* this line */ $Assist = ($PRTRow["AssistingStaff"]==0) ? "Unknown" : StaffName($PRTRow["AssistingStaff"]); /* Goes to function StaffName anyways */ $PRT[] = array("PNCID" => (int)$PRTRow["PNCID"], "DTG" => strtotime($PRTRow["DTG"]), "Title" => "PNCID " . $PRTRow["PNCID"] . "; " . date("d M Y", strtotime($PRTRow["DTG"])) ."; " . $CadetName . "; Applied By:" . StaffName($PRTRow["AB"]) . "; Assisted By:" . StaffName($PRTRow["AssistingStaff"]) ); } } //sort($PRT); return $PRT; } Quote Link to comment https://forums.phpfreaks.com/topic/120726-solved-undefined-variable/ Share on other sites More sharing options...
JonnoTheDev Posted August 21, 2008 Share Posted August 21, 2008 Your $StaffName variable is created inside a loop from a DB query meaning that if the query produces 0 results then the variable is not defined. If you are returning a variable in a function you should always define it near the top of the function: $StaffName = ""; // run query return $StaffName; Quote Link to comment https://forums.phpfreaks.com/topic/120726-solved-undefined-variable/#findComment-622116 Share on other sites More sharing options...
uniflare Posted August 21, 2008 Share Posted August 21, 2008 or, you can return false if it is not defined: return (isset($StaffName))? $StaffName : false; Returns staffname on success, or false if no results Quote Link to comment https://forums.phpfreaks.com/topic/120726-solved-undefined-variable/#findComment-622162 Share on other sites More sharing options...
jrm Posted August 21, 2008 Author Share Posted August 21, 2008 Well there in lies the problem that StaffName() should not ever be called due to the statement: $Assist = ($PRTRow["AssistingStaff"]==0) ? "Unknown" : StaffName($PRTRow["AssistingStaff"]); Am I correct in saying this??? Quote Link to comment https://forums.phpfreaks.com/topic/120726-solved-undefined-variable/#findComment-622220 Share on other sites More sharing options...
uniflare Posted August 21, 2008 Share Posted August 21, 2008 seems the if statement isnt functioning as you would expect. try using $Assist = ($PRTRow["AssistingStaff"]=="0") ? "Unknown" : StaffName($PRTRow["AssistingStaff"]); also try this to debug: while($PRTRow = mysql_fetch_array($PRTResults, MYSQL_ASSOC)) { $PNCQuery = "Select CadetID From PNC_be.PNC Where PNCID = " . $PRTRow["PNCID"] .";"; $PNCResults = mysql_query($PNCQuery, $link) or die("\n<p>Could not run PNCQuery : " . $PNCQuery ." : " . mysql_error()); if(mysql_num_rows($PNCResults)==0) { $CadetID = 107;} else { $PNCRow = mysql_fetch_array($PNCResults, MYSQL_ASSOC); $CadetID = $PNCRow["CadetID"]; } $CadetName = CadetName($CadetID); $AB = ($PRTRow["AB"]==0) ? "Unknown" : StaffName($PRTRow["AB"]); /* this line */ print_r($PRTRow); $Assist = ($PRTRow["AssistingStaff"]==0) ? "Unknown" : StaffName($PRTRow["AssistingStaff"]); /* Goes to function StaffName anyways */ $PRT[] = array("PNCID" => (int)$PRTRow["PNCID"], "DTG" => strtotime($PRTRow["DTG"]), "Title" => "PNCID " . $PRTRow["PNCID"] . "; " . date("d M Y", strtotime($PRTRow["DTG"])) ."; " . $CadetName . "; Applied By:" . StaffName($PRTRow["AB"]) . "; Assisted By:" . StaffName($PRTRow["AssistingStaff"]) ); } exit(); At a glance i would guess the problem is that the value of $PRTRow["AssistingStaff"] is a string instead of a integer, so the string "0" should be true. Try my examples, the second you will need to view the source for the answers, it will output the array neatly but it uses \n newline and not < br > html so it's easier to read in the html source file. hope this helps, Quote Link to comment https://forums.phpfreaks.com/topic/120726-solved-undefined-variable/#findComment-622238 Share on other sites More sharing options...
jrm Posted August 21, 2008 Author Share Posted August 21, 2008 No, that still did not work and I also type cast it to force it to a (int), but still it did not work. So I bypast the while loop in StaffName() by adding a if then clause counting the mysql_num _rows() from the mysql_query. I don't like it because it did not solve the problem, but it is a work around and I don't see that message anymore. Quote Link to comment https://forums.phpfreaks.com/topic/120726-solved-undefined-variable/#findComment-622258 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.