Undefined offset: 1

五迷三道 提交于 2019-12-10 21:08:14

问题


In my current PHP script this error comes up: Undefined offset: 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! :)


回答1:


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'] = '';



回答2:


the error is on this line:

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



回答3:


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.



来源:https://stackoverflow.com/questions/5424236/undefined-offset-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!