久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    1. <legend id='FHfrI'><style id='FHfrI'><dir id='FHfrI'><q id='FHfrI'></q></dir></style></legend>
    2. <small id='FHfrI'></small><noframes id='FHfrI'>

      <tfoot id='FHfrI'></tfoot>

          <bdo id='FHfrI'></bdo><ul id='FHfrI'></ul>
        <i id='FHfrI'><tr id='FHfrI'><dt id='FHfrI'><q id='FHfrI'><span id='FHfrI'><b id='FHfrI'><form id='FHfrI'><ins id='FHfrI'></ins><ul id='FHfrI'></ul><sub id='FHfrI'></sub></form><legend id='FHfrI'></legend><bdo id='FHfrI'><pre id='FHfrI'><center id='FHfrI'></center></pre></bdo></b><th id='FHfrI'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='FHfrI'><tfoot id='FHfrI'></tfoot><dl id='FHfrI'><fieldset id='FHfrI'></fieldset></dl></div>

        函數中的 PDO try-catch 用法

        PDO try-catch usage in functions(函數中的 PDO try-catch 用法)

        <small id='di76f'></small><noframes id='di76f'>

            <i id='di76f'><tr id='di76f'><dt id='di76f'><q id='di76f'><span id='di76f'><b id='di76f'><form id='di76f'><ins id='di76f'></ins><ul id='di76f'></ul><sub id='di76f'></sub></form><legend id='di76f'></legend><bdo id='di76f'><pre id='di76f'><center id='di76f'></center></pre></bdo></b><th id='di76f'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='di76f'><tfoot id='di76f'></tfoot><dl id='di76f'><fieldset id='di76f'></fieldset></dl></div>

              • <bdo id='di76f'></bdo><ul id='di76f'></ul>
                • <legend id='di76f'><style id='di76f'><dir id='di76f'><q id='di76f'></q></dir></style></legend>
                    <tbody id='di76f'></tbody>
                  <tfoot id='di76f'></tfoot>
                • 本文介紹了函數中的 PDO try-catch 用法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在考慮在我未來的所有 web 應用程序中使用 PDO.目前(使用到目前為止我從 SO 中學到的知識),我在我的站點中處理數據庫連接的是一個像這樣的 Singleton 類:

                  I'm thinking of using PDO in all of my future webapp. Currently (using what I've learned from SO so far), what I have in my site to handle database connection is a Singleton class like this :

                  class DB {
                  
                      private static $instance = NULL;
                      private static $dsn      = "mysql:host=localhost;dbname=mydatabase;";
                      private static $db_user  = 'root';
                      private static $db_pass  = '0O0ooIl1';
                  
                      private function __construct() 
                      {
                  
                      }
                      private function __clone()
                      {
                  
                      }   
                      public static function getInstance() {
                  
                          if (!self::$instance)
                          {           
                              self::$instance = new PDO(self::$dsn, self::$db_user, self::$db_pass);
                              self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                          }
                          return self::$instance;
                      }
                  }
                  

                  和另一個文件 (functions.php) 與內容特定的函數看起來完全一樣:

                  and another file (functions.php) with content-specific functions looking exactly like this one :

                  function get_recent_activities ()
                  {    
                      try
                      {    
                          $db = DB::getInstance();
                          // --prepare and execute query here, fetch the result--
                          return $my_list_of_recent_activities;
                      }
                      catch (PDOException $e)
                      {
                          return "some fail-messages";
                      }
                  }
                  ...
                  

                  意味著我必須在所有函數中重復 try .. catch 部分.

                  meaning that I have to repeat the try .. catch part in all of the functions.

                  我的問題是:

                  1. 我應該如何提高效率?(例如,不必在所有函數中重復 try..catch,但仍然能夠在每個函數上返回不同的失敗消息")
                  2. 這已經是一個好習慣了嗎?我還是 PDO 和 OOP 的新手(還有很多東西要學習),所以(截至目前),我真的看不出有什么缺點或可以改進的地方.
                  1. How should I make that more efficient ? (eg. not having to repeat try..catch in all functions, and yet still able to return different "fail-message" on each one)
                  2. Is this already a good practice ? I'm still new at PDO and OOP (still a lot more to learn), so (as of now), I can't really see any disadvantages or things that can be improved in there.

                  如果這看起來不清楚或太長,我很抱歉.提前致謝.

                  I'm sorry if that seems unclear or too long. Thanks in advance.

                  推薦答案

                  你的實現很好,而且在大多數情況下都能很好地工作.

                  Your implementation is just fine, and it'll work perfectly well for most purposes.

                  沒有必要將每個查詢都放在 try/catch 塊中,事實上,在大多數情況下,您實際上并不想這樣做.這樣做的原因是,如果查詢生成異常,則是語法錯誤或數據庫問題等致命問題的結果,而這些不是您在執行每個查詢時都應該考慮的問題.

                  It's not necessary to put every query inside a try/catch block, and in fact in most cases you actually don't want to. The reason for this is that if a query generates an exception, it's the result of a fatal problem like a syntax error or a database issue, and those are not issues that you should be accounting for with every query that you do.

                  例如:

                  try {
                      $rs = $db->prepare('SELECT * FROM foo');
                      $rs->execute();
                      $foo = $rs->fetchAll();
                  } catch (Exception $e) {
                      die("Oh noes! There's an error in the query!");
                  }
                  

                  這里的查詢要么正常工作,要么根本不工作.它根本不起作用的情況在生產系統上不應該有規律地發生,所以它們不是您應該在這里檢查的條件.這樣做實際上適得其反,因為您的用戶會收到永遠不會改變的錯誤,而您不會收到提醒您問題的異常消息.

                  The query here will either work properly or not work at all. The circumstances where it wouldn't work at all should not ever occur with any regularity on a production system, so they're not conditions that you should check for here. Doing so is actually counterproductive, because your users get an error that will never change, and you don't get an exception message that would alert you to the problem.

                  相反,只需這樣寫:

                  $rs = $db->prepare('SELECT * FROM foo');
                  $rs->execute();
                  $foo = $rs->fetchAll();
                  

                  一般來說,您唯一需要捕獲和處理查詢異常的時間是在查詢失敗時您想執行其他操作的時候.例如:

                  In general, the only time that you'll want to catch and handle a query exception is when you want to do something else if the query fails. For example:

                  // We're handling a file upload here.
                  try {
                      $rs = $db->prepare('INSERT INTO files (fileID, filename) VALUES (?, ?)');
                      $rs->execute(array(1234, '/var/tmp/file1234.txt'));
                  } catch (Exception $e) {
                      unlink('/var/tmp/file1234.txt');
                      throw $e;
                  }
                  

                  您需要編寫一個簡單的異常處理程序,用于記錄或通知您生產環境中發生的數據庫錯誤,并向您的用戶顯示友好的錯誤消息而不是異常跟蹤.請參閱 http://www.php.net/set-exception-handler 了解有關怎么做.

                  You'll want to write a simple exception handler that logs or notifies you of database errors that occur in your production environment and displays a friendly error message to your users instead of the exception trace. See http://www.php.net/set-exception-handler for information on how to do that.

                  這篇關于函數中的 PDO try-catch 用法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)
                  <i id='MOvPW'><tr id='MOvPW'><dt id='MOvPW'><q id='MOvPW'><span id='MOvPW'><b id='MOvPW'><form id='MOvPW'><ins id='MOvPW'></ins><ul id='MOvPW'></ul><sub id='MOvPW'></sub></form><legend id='MOvPW'></legend><bdo id='MOvPW'><pre id='MOvPW'><center id='MOvPW'></center></pre></bdo></b><th id='MOvPW'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='MOvPW'><tfoot id='MOvPW'></tfoot><dl id='MOvPW'><fieldset id='MOvPW'></fieldset></dl></div>

                          <small id='MOvPW'></small><noframes id='MOvPW'>

                            <tbody id='MOvPW'></tbody>
                          • <bdo id='MOvPW'></bdo><ul id='MOvPW'></ul>
                            <tfoot id='MOvPW'></tfoot>
                            <legend id='MOvPW'><style id='MOvPW'><dir id='MOvPW'><q id='MOvPW'></q></dir></style></legend>
                            主站蜘蛛池模板: 国产精品久久久久久久久久东京 | 精品国产一区二区在线 | 国产精品毛片一区二区在线看 | av日韩在线播放 | 伊人春色成人 | 国产精品美女一区二区 | 91精品国产91久久久久久密臀 | 九九九视频精品 | 91精品国产高清一区二区三区 | 羞羞视频在线观看 | 91私密视频 | 亚洲 欧美 激情 另类 校园 | 91免费在线 | 日韩成人在线看 | av男人的天堂av | 亚洲国产一区在线 | 国产精品视频一二三区 | 欧美三级在线 | 国产精品视频偷伦精品视频 | 午夜免费在线电影 | 午夜欧美一区二区三区在线播放 | 九九久久国产 | 一区在线视频 | 亚洲视频在线播放 | 成人精品国产免费网站 | 国产精品久久久久无码av | 欧美日韩一区二区三区四区 | 国产精品久久 | www.成人在线视频 | 人人鲁人人莫人人爱精品 | tube国产 | 国产一区二区三区在线 | 日日噜噜噜夜夜爽爽狠狠视频, | 国产精品亚洲精品久久 | 中文字幕亚洲一区二区三区 | 日韩中文字幕一区二区 | 欧美精选一区二区 | 一级黄色片免费在线观看 | 午夜久草| 一区二区在线看 | 欧美偷偷操 |