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

      • <bdo id='U8i1T'></bdo><ul id='U8i1T'></ul>

        <i id='U8i1T'><tr id='U8i1T'><dt id='U8i1T'><q id='U8i1T'><span id='U8i1T'><b id='U8i1T'><form id='U8i1T'><ins id='U8i1T'></ins><ul id='U8i1T'></ul><sub id='U8i1T'></sub></form><legend id='U8i1T'></legend><bdo id='U8i1T'><pre id='U8i1T'><center id='U8i1T'></center></pre></bdo></b><th id='U8i1T'></th></span></q></dt></tr></i><div id='U8i1T'><tfoot id='U8i1T'></tfoot><dl id='U8i1T'><fieldset id='U8i1T'></fieldset></dl></div>
      1. <legend id='U8i1T'><style id='U8i1T'><dir id='U8i1T'><q id='U8i1T'></q></dir></style></legend>

      2. <tfoot id='U8i1T'></tfoot>
      3. PHP 和 Postgres:捕捉错误?

        时间:2023-09-21
        <i id='fU9u6'><tr id='fU9u6'><dt id='fU9u6'><q id='fU9u6'><span id='fU9u6'><b id='fU9u6'><form id='fU9u6'><ins id='fU9u6'></ins><ul id='fU9u6'></ul><sub id='fU9u6'></sub></form><legend id='fU9u6'></legend><bdo id='fU9u6'><pre id='fU9u6'><center id='fU9u6'></center></pre></bdo></b><th id='fU9u6'></th></span></q></dt></tr></i><div id='fU9u6'><tfoot id='fU9u6'></tfoot><dl id='fU9u6'><fieldset id='fU9u6'></fieldset></dl></div>

          <legend id='fU9u6'><style id='fU9u6'><dir id='fU9u6'><q id='fU9u6'></q></dir></style></legend>

            • <bdo id='fU9u6'></bdo><ul id='fU9u6'></ul>

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

                    <tbody id='fU9u6'></tbody>
                  <tfoot id='fU9u6'></tfoot>
                  本文介绍了PHP 和 Postgres:捕捉错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如果代码失败,我应该如何准备代码?使用 try-catch 语句还是?

                  function delete_question ( $question_id ) {
                      $dbconn = pg_connect("host=localhost port=5432 dbname=heoa user=heoa password=123");
                  
                      // removes questions and its dependencies: answers and tags
                      $result = pg_query_params ( $dbconn,
                          'DELETE FROM questions
                          WHERE question_id = $1',
                          array ( $question_id )
                      );
                  

                  推荐答案

                  如果你想要异常,那么你需要使用 PDO.

                  If you want exceptions, then you need to use PDO.

                  对于 pg_* 函数和您的代码,您需要检查 $result 是否具有 false 值,如果是,则发生错误.

                  in case of pg_* functions and your code, you need to check whether $result has the value of false, if it does, then an error occured.

                  您可以使用 pg_last_error() 获取错误描述

                  You can get the error description with pg_last_error()

                  像这样:

                  $result = pg_query_params ( $dbconn,
                          'DELETE FROM questions
                          WHERE question_id = $1',
                          array ( $question_id )
                      );
                  
                  
                  if ($result === false) {
                      print pg_last_error($dbconn);
                  } else {
                      print 'everything was ok';
                  }
                  

                  所以,基本上,每次使用 pg_* 函数时,都需要检查是否返回了 false,这些函数就是这样.

                  So, basically, every time you use a pg_* function, you need to check whether false was returned, that's just the way it is with those functions.

                  是的,您可以构建自己的包装器,因此您可以调用 my_db_query() 而不是 pg_query*,然后它会执行返回值检查和异常抛出.

                  Yes, you can build your own wrappers so instead of pg_query* you call my_db_query(), which then does the return value checking and exception throwing.

                  或者,您可以使用 PDO,它可以针对可能出现的所有错误向您抛出 PDOException.

                  Or, you could go with PDO, which is able to throw you PDOException for all the errors that can occour.

                  这篇关于PHP 和 Postgres:捕捉错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:当 fopen() 失败时获取有意义的信息 (PHP/suPHP) 下一篇:PHP 在验证错误后保留输入的值

                  相关文章

                  最新文章

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

                1. <tfoot id='L6m2s'></tfoot>
                    <bdo id='L6m2s'></bdo><ul id='L6m2s'></ul>
                  <legend id='L6m2s'><style id='L6m2s'><dir id='L6m2s'><q id='L6m2s'></q></dir></style></legend>

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