【问题标题】:To check `0 rows` from the SQL query in Postgres / PHP从 Postgres / PHP 中的 SQL 查询中检查“0 行”
【发布时间】:2009-08-18 16:16:51
【问题描述】:

如何检查一个空的数据库结果,然后基于此执行操作?

我的代码将匹配结果放入 if -clause

 while( $tags_and_Qid = pg_fetch_array( $result_tags )) {
      // Add the Tag to an array of tags for that question
      $end_array [ $tags_and_Qid['question_id'] ] ['tag'] [] = $tags_and_Qid['tag'];


  }

if ( $end_array [ $tags_and_Qid['question_id'] ] ['tag'] == '' ) {                                                                    
          // Problem here!                                                                       
    header( "Location: index.php?"
        . "no_question_found"
      ); 
  }

【问题讨论】:

    标签: php database postgresql


    【解决方案1】:
    if(count($end_array) == 0)
    {
        // No Results
    }
    

    我喜欢检查我正在使用 foreach 的数组,以检查它是一个有效的数组而不是数据库资源。这是因为我可能会选择在抓取数据后过滤掉一些东西,如果我检查数据库返回的内容,它不如检查我计划处理的数组准确。

    您也可以这样做。

    if(pg_num_rows($result_tags) == 0)
    {
        // Num Rows
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用pg_num_rows(),例如

      if (!pg_num_rows( $result_tags )) {
          // Problem here!                                                                       
          header( "Location: index.php?no_question_found");
          exit();
      } 
      while( $tags_and_Qid = pg_fetch_array( $result_tags )) {
            // Add the Tag to an array of tags for that question
            $end_array [ $tags_and_Qid['question_id'] ] ['tag'] [] = $tags_and_Qid['tag'];
      }
      

      【讨论】:

        猜你喜欢
        • 2015-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多