错误处理php_network_getaddresses:getaddrinfo失败。怎么样?

时间:2017-08-03 16:02:12

标签: php mysql ajax

我正在尝试抛出异常并捕获错误然后回显一个数字。

这是我到目前为止所做的,但它返回以下PHP错误,这反过来使我的AJAX beforeSend请求挂起。

PHP代码

<?php  

  /* Status Codes

     return 0 = Nothing to Update
     return 1 = Successful Update Query
     return 2 = Database Connection refused
     return 3 = MySQL Query Error OR Wrong URL Parameters */

  if(isset($_GET["postT_VAL"])) {

  $client_id    = $_GET["postCLIENT_ID"];
  $project_id   = $_GET["postPROJECT_ID"];
  $mainsheet_id = $_GET["postMAINSHEET_ID"];
  $field_name = $_GET["postT_ID"];
  $field_value = $_GET["postT_VAL"];

  if(!$link = mysqli_connect("intentionally_mispelled", "correct_user", "correct_pass", "correct_database")) {
    echo "2";
    exit;

  } else {
  /* Build dynamic Update Query string */
  $sql = "UPDATE tbl_mainsheet2 SET ".$field_name." = '".$field_value."' WHERE client_id = '".$client_id."' AND project_id = '".$project_id."' AND mainsheet_id = '".$mainsheet_id."'";  

  /* Execute Update Query */    
  if(!mysqli_query($link, $sql)) {

  echo "3";
  /* Close Connection */
  mysqli_close($link);

  exit;

  } else {

  /* return 0 = Nothing to Update / 1 = Successful Update Query */
  echo "".mysqli_affected_rows($link);

  /* Close Connection */
  mysqli_close($link);

  }
  }

 }

?>

如上所述,我如何优雅地处理此错误?

<br />
    <b>Warning</b>:  mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in <b>/nfs/c12/h02/mnt/220474/domains/site.com/html/autosave4/processor.php</b> on line <b>20</b><br />
    <br />
    <b>Fatal error</b>:  Uncaught exception 'mysqli_sql_exception' with message 'php_network_getaddresses: getaddrinfo failed: Name or service not known' in /nfs/c12/h02/mnt/220474/domains/nexlevel.org/html/autosave4/processor.php:20
    Stack trace:
    #0 /nfs/c12/h02/mnt/220474/domains/nexlevel.org/html/autosave4/processor.php(20): mysqli_connect('intentionally_mispelled', 'correct_username', 'correct_password', 'correct_database')
    #1 {main}
      thrown in <b>/nfs/c12/h02/mnt/220474/domains/site.com/html/autosave4/processor.php</b> on line <b>20</b><br />

基本上我只想回显一个简单的数字2,但PHP会中断我的回声并在下面输出这个错误堆栈。

最终法典 - 工作&amp; COMMENTED

<?php  

  /* Status Codes

     return 0 = Nothing to Update
     return 1 = Successful Update Query
     return 2 = Database Connection refused
     return 3 = MySQL Query Error OR Wrong URL Parameters */

  /* Sample URL */
  // mysite.org/autosave5/processor.php?postCLIENT_ID=111&postPROJECT_ID=222&postMAINSHEET_ID=333&postT_ID=WTRESRVD&postT_VAL=147

  /* Disable Warnings so that we can return ONLY what we want through echo. */
  mysqli_report(MYSQLI_REPORT_STRICT);

  if(isset($_GET["postT_VAL"])) {

  // Initialize Global variables.
  $client_id    = '';
  $project_id   = '';
  $mainsheet_id = '';
  $field_name   = '';
  $field_value  = '';

  /* Database Connection Check */
  try
  {
    if ($link = mysqli_connect("incorrect_domain", "correct_username", "correct_password", "correct_database"))
    {

      // Set and Escape Global variables.
      $client_id    = mysqli_real_escape_string($link, $_GET["postCLIENT_ID"]);
      $project_id   = mysqli_real_escape_string($link, $_GET["postPROJECT_ID"]);
      $mainsheet_id = mysqli_real_escape_string($link, $_GET["postMAINSHEET_ID"]);
      $field_name = mysqli_real_escape_string($link, $_GET["postT_ID"]);
      $field_value = mysqli_real_escape_string($link, $_GET["postT_VAL"]);

      /* Build dynamic Update Query string */
      $sql = "UPDATE tbl_mainsheet2 SET ".$field_name." = '".$field_value."' WHERE client_id = '".$client_id."' AND project_id = '".$project_id."' AND mainsheet_id = '".$mainsheet_id."'";  

      /* Execute Update Query */    
      if(!mysqli_query($link, $sql)) {
        /* return 3 = MySQL Query Error OR Wrong URL Parameters */
        echo "3";
        /* Close Connection */
        mysqli_close($link);
        exit;
      } else {
        /* return 0 = Nothing to Update / 1 = Successful Update Query */
        echo "".mysqli_affected_rows($link);
        /* Close Connection */
        mysqli_close($link);
     }

    } else {
      throw new Exception('2');
    }
  } catch(Exception $e) {
    /* echo $e->getMessage();
       return 2 = Database Connection refused */
    echo "2";
  }

 }

?>

1 个答案:

答案 0 :(得分:1)

如果你想捕获并抛出异常,请不要使用这种if语句。使用try ... catch

try
{
  if ($db = mysqli_connect($hostname_db, $username_db, $password_db, $base))
  {
    //do something
  }
  else
  {
      throw new Exception('Unable to connect');
  }
}
catch(Exception $e)
{
  echo $e->getMessage();
}