Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/187649-function-in-array-error/
Share on other sites

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.

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);
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.