I am a little rusty in PHP after several years without programming with this language.
I am fetching data from an array in mySQL using PHP.
My problem is that at some point I fetch data from a table, using this:
$myArray = mysql_fetch_array( $data );
but at some point, the data base may be empty, so $myArray will be null or contain no elements.
Then I have to check if a particular string that is generated on-the-fly is on $myArray. If it is, another string must be generated and verified if it already exists on $myArray.
$myString = generateString();
if (in_array($myString, $myArray)) {
$myString = generateString();
}
this code gives me this error:
Warning: in_array() [function.in-array]: Wrong datatype for second argument
To prevent in_array from running when $myArray is empty I did this:
if (count($myArray) > 0) {
if (in_array($myString, $myArray)) {
$myString = generateString();
}
}
but count is giving me 1 when the array is empty (?)…
How do I solve that?
just another question: $myString is being modified inside the IF that is already testing it. Is this possible in PHP?
Well, if your query failed, you’ll have… wait for it… no array!
So that
in_arraycall would give you a warning about$myArraynot being an array (butfalse).You need to check your query for errors, and better yet: