簡體   English   中英

如何遍歷php錯誤並繼續while循環?

[英]How to iterate over php error and continue while loop?

我根據數據庫中的用戶名,使用以下代碼從遠程網站解析定義。 我正在使用簡單的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請求失敗! 在第75行的/app/simple_html_dom.php中找不到HTTP / 1.0 404致命錯誤:在第18行的/app/test.php中的布爾值上調用成員函數find()

我的問題是如何跳過引發錯誤的用戶名並繼續循環? 另外,由於它們的用戶名超過500個,因此該操作非常耗時,因此我要保存定義,並希望避免將錯誤保存到數據庫中。

您可以抑制警告,如以下鏈接所示

如何處理PHP中的file_get_contents()函數警告?

或者另一種選擇是編寫自定義錯誤處理程序。 看到這個鏈接

PHP:如何使用set_error_handler()正確處理除通知外的所有錯誤?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM