Reputation: 35953
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?
Upvotes: 0
Views: 2504
Reputation: 174977
Well, if your query failed, you'll have... wait for it... no array!
So that in_array
call would give you a warning about $myArray
not being an array (but false
).
You need to check your query for errors, and better yet:
Please, don't use
mysql_*
functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Upvotes: 2