如何迭代php错误并继续循环?

时间:2017-08-22 16:42:17

标签: php database mysqli

根据我数据库中的用户名,我使用以下代码来解析远程网站的定义。我使用简单的html dom解析器。

//database connection
include 'db.php';
//display errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include 'simple_html_dom.php';

//select usernames order alphabetically
$row = mysqli_query($conn, "SELECT user_name FROM (
  SELECT * 
  FROM store 
  ORDER BY user_id DESC) 
  AS store ORDER BY user_id ASC");

//echo out definitions of usernames
while($lastusers = mysqli_fetch_array($row)) {
    echo '<br>' . $lastusers["user_name"];
    echo '<br>Definition:<br>';
    $html = file_get_html("https://www.merriam-webster.com/dictionary/" . $lastusers["user_name"]);
    $title = $html->find("div.card-primary-content", 0)->innertext;
    echo $title;

    //save definitions to corresponding username 
    $save = mysqli_query($conn, 'UPDATE  store
    SET     def = $title,
    WHERE   user_name = $lastusers["user_name"]'
    )
}

我的while循环将开始为每个用户名生成定义,直到找不到某些页面,从而导致出现停止循环的错误。

  

警告:   的file_get_contents(https://www.merriam-webster.com/dictionary/colourings):   无法打开流:HTTP请求失败!找不到HTTP / 1.0 404   第75行/app/simple_html_dom.php致命错误:致电会员   在第18行的/app/test.php中的boolean函数中找到find()

我的问题是如何跳过抛出错误的用户名并继续循环?另外因为它们的用户名超过500个,所以这个操作非常密集,因此我保存了定义,并希望避免将错误保存到数据库中。

1 个答案:

答案 0 :(得分:0)

您可以取消警告,如以下链接所示

How can I handle the warning of file_get_contents() function in PHP?

或者另一种方法是编写自定义错误处理程序。看到这个链接

PHP: How to use set_error_handler() to properly deal with all errors except notices?

相关问题