thefollower Posted January 7, 2010 Share Posted January 7, 2010 Hey im having problems it an in_array check im doing im not entirely sure what is wrong so im better off showing u the script. The idea is to check the user has completed some thing by checking it against an id loaded from a table - im using a function because its easier in the long run for future scripts that also may need to do similiar checks. <?php function completedresearch($UserID){ $Get = mysql_query("SELECT ResearchID FROM research WHERE Completed='1'") Or die(mysql_error()); return (mysql_fetch_assoc($Get)); } //query to get research id If(in_array($row['ResearchID'],completedresearch($_SESSION['Current_User']))){ Echo 'yes it is in the array'; } ?> The error i get is: Warning: in_array() [function.in-array]: Wrong datatype for second argument in functions.php on line 979 Quote Link to comment https://forums.phpfreaks.com/topic/187649-function-in-array-error/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2010 Share Posted January 8, 2010 Try using var_dump on what completedresearch($_SESSION['Current_User']) returns to see what you are actually getting. Also try using var_dump inside the function on what mysql_fetch_assoc($Get) returns. If you are getting an array inside the function but at getting a NULL where the function is called, it is because php has a problem returning the value of some of the built-in functions (I have only seen this with a few of the mysql_ functions.) If this is what is occurring, you need to assign mysql_fetch_assoc($Get) to a variable, then use that variable in the return statement. If you are getting bool(FALSE) values both inside and outside the function , then the query executed but there were zero matching rows. Your function needs some additional logic to cause it to behave in an expected way in the case where there are zero matching rows in the result set. It will currently return a FALSE value which will cause an error in the in_array() function. You should probably return an empty array in this case. Your current code in the function will, when the query finds any matching row(s) in the table, return an array with only one single entry that is the first row in the result set. Quote Link to comment https://forums.phpfreaks.com/topic/187649-function-in-array-error/#findComment-990698 Share on other sites More sharing options...
thefollower Posted January 8, 2010 Author Share Posted January 8, 2010 Oh - turns out no rows returned like you suggested, so mysql_fetch_assoc is nothing... so what can i do to return something accurate in this situation that won't give an error? I edited the function to this to test it: <?php function completedresearch($UserID){ $Get = mysql_query("SELECT ResearchID FROM mayor_research WHERE Completed='1'") Or die(mysql_error()); If(mysql_num_rows($Get)<1){ Echo ' no rows'; } $row = mysql_fetch_assoc($Get); return ($row); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/187649-function-in-array-error/#findComment-990699 Share on other sites More sharing options...
thefollower Posted January 8, 2010 Author Share Posted January 8, 2010 bump - post ended up on a entirely different page. Quote Link to comment https://forums.phpfreaks.com/topic/187649-function-in-array-error/#findComment-990738 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.