2017-08-03 118 views
0

我想拋出一個異常,並捕獲一個錯誤,然後回顯一個數字。錯誤處理php_network_getaddresses:getaddrinfo失敗。怎麼樣?

這是我到目前爲止,但它返回以下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打斷了我的回聲和輸出低於這個錯誤堆棧。

最終代碼 - 工作& 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"; 
    } 

} 

?> 
+0

您的代碼很容易受到[** SQL注入**](https://en.wikipedia.org/wiki/SQL_injection)攻擊。您應該通過[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)或[** PDO **](https ://secure.php.net/manual/en/pdo.prepared-statements.php)驅動程序。 [**這篇文章**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)有一些很好的例子。 –

+0

剛剛更新!工作代碼發佈供所有人學習。 – ASPiRE

回答

1

如果你想趕上並拋出exceptionm不要使用這種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(); 
} 
+0

你。剛剛以我現在真正瞭解它的方式展示了它。就一個問題。拋出新的異常('無法連接');需要說'無法連接'或者它可以是空的? – ASPiRE

+0

文字'無法連接'可以是你想要的。您可以將其設置爲空,或者使用自定義消息編寫一個字符串 – sheplu

+0

正確,但似乎顯示的內容不是回寫在異常中的內容。 – ASPiRE

相關問題