php 未定义偏移:1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5424236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:21:47  来源:igfitidea点击:

Undefined offset: 1

phphtmloffset

提问by user653480

In my current PHP script this error comes up: Undefined offset: 1

在我当前的 PHP 脚本中出现此错误:未定义偏移:1

My code is here:

我的代码在这里:

$query = "SELECT item_id, username, item_content FROM updates ORDER BY update_time DESC                 LIMIT " . $start . ", " . $number_of_posts;

    $result = mysql_query($query) or die(mysql_error());        
    while($row = mysql_fetch_assoc($result)) {
            preg_match("/<p>(.*)<\/p>/",$row['item_content'],$matches);
            $row['item_content'] = strip_tags($matches[1]);
            $posts[] = $row;
        }

If you see whats causing this, posting below would really help. Thanks! :)

如果您看到导致这种情况的原因,则在下面发布确实会有所帮助。谢谢!:)

回答by Frosty Z

Instead of

代替

$row['item_content'] = strip_tags($matches[1]);

Try

尝试

if (isset($matches[0]) && isset($matches[0][1]))
  $row['item_content'] = strip_tags($matches[0][1]);
else
  $row['item_content'] = '';

回答by Naftali aka Neal

the error is on this line:

错误在这一行:

$row['item_content'] = strip_tags($matches[1]);

回答by Dhaivat Pandya

As Neal said, there is a problem on that line. It seems that $matches[1] is undefined (i.e. there are no mathces, or there is only 1 match, $matches[0]). Make sure your table is working correctly.

正如尼尔所说,那条线上有问题。似乎 $matches[1] 是未定义的(即没有 mathces,或者只有 1 个匹配,$matches[0])。确保您的桌子工作正常。