如何检查空数据库结果,然后根据此进行操作?
我的代码将匹配结果放入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"
);
}
答案 0 :(得分:2)
if(count($end_array) == 0)
{
// No Results
}
我喜欢检查我使用foreach的数组,以检查它是否是一个有效的数组而不是数据库资源。这是因为我可能会选择在获取数据后过滤掉一些东西,如果我检查数据库返回的内容,则不如检查我计划处理的数组那么准确。
你也可以这样做。
if(pg_num_rows($result_tags) == 0)
{
// Num Rows
}
答案 1 :(得分:1)
您可以使用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'];
}