douwei9759 2012-11-18 21:56
浏览 41
已采纳

警告:in_array()[function.in-array]:第二个参数的数据类型错误

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?

  • 写回答

1条回答 默认 最新

  • dsyo9700 2012-11-18 21:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!