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.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分