From 575e3e7d38749fe76cf071a46685faab75dc68bd Mon Sep 17 00:00:00 2001 From: Paul Richards Date: Sat, 6 Sep 2014 01:45:06 +0100 Subject: [PATCH] Remove use of db_num_rows for select queries which return data i.e. db_fetch_array is also called following the call of db_num_rows --- core/custom_field_api.php | 23 +++++++++--------- core/filter_api.php | 32 ++++++++------------------ core/print_api.php | 13 ++++------- core/project_api.php | 29 +++++++++++------------ core/sponsorship_api.php | 11 +++++---- core/tag_api.php | 4 ++-- core/user_api.php | 6 +---- lost_pwd.php | 4 ++-- plugins/MantisGraph/core/graph_api.php | 20 ++++------------ print_all_bug_options_inc.php | 7 ++++-- 10 files changed, 59 insertions(+), 90 deletions(-) diff --git a/core/custom_field_api.php b/core/custom_field_api.php index 4b191b51b9..7a1288a05e 100644 --- a/core/custom_field_api.php +++ b/core/custom_field_api.php @@ -918,12 +918,12 @@ function custom_field_get_sequence( $p_field_id, $p_project_id ) { project_id=' . db_param(); $t_result = db_query( $t_query, array( $p_field_id, $p_project_id ), 1 ); - if( 0 == db_num_rows( $t_result ) ) { + $t_row = db_fetch_array( $t_result ); + + if( !$t_row ) { return false; } - $t_row = db_fetch_array( $t_result ); - return $t_row['sequence']; } @@ -1082,19 +1082,19 @@ function custom_field_distinct_values( array $p_field_def, $p_project_id = ALL_P FROM ' . $t_from . ' WHERE ' . $t_where1 . $t_where2 . ' ORDER BY cfst.value'; - $t_result = db_query( $t_query, $t_params ); - $t_row_count = db_num_rows( $t_result ); - if( 0 == $t_row_count ) { - return false; - } + $t_row_count = 0; - for( $i = 0;$i < $t_row_count;$i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { + $t_row_count++; if( !is_blank( trim( $t_row['value'] ) ) ) { array_push( $t_return_arr, $t_row['value'] ); } } + + if( 0 == $t_row_count ) { + return false; + } } return $t_return_arr; } @@ -1178,7 +1178,7 @@ function custom_field_set_value( $p_field_id, $p_bug_id, $p_value, $p_log_insert bug_id=' . db_param(); $t_result = db_query( $t_query, array( $p_field_id, $p_bug_id ) ); - if( db_num_rows( $t_result ) > 0 ) { + if( $t_row = db_fetch_array( $t_result ) ) { $t_query = 'UPDATE {custom_field_string} SET ' . $t_value_field . '=' . db_param() . ' WHERE field_id=' . db_param() . ' AND @@ -1190,7 +1190,6 @@ function custom_field_set_value( $p_field_id, $p_bug_id, $p_value, $p_log_insert ); db_query( $t_query, $t_params ); - $t_row = db_fetch_array( $t_result ); history_log_event_direct( $p_bug_id, $t_name, custom_field_database_to_value( $t_row[$t_value_field], $t_type ), $p_value ); } else { $t_query = 'INSERT INTO {custom_field_string} diff --git a/core/filter_api.php b/core/filter_api.php index 83af4b89f4..dd7a2549cd 100644 --- a/core/filter_api.php +++ b/core/filter_api.php @@ -2056,11 +2056,9 @@ function filter_get_bug_rows( &$p_page_number, &$p_per_page, &$p_page_count, &$p } $t_result = db_query( $t_select_string . $t_from_string . $t_join_string . $t_where_string . $t_order_string, $t_query_clauses['where_values'], $p_per_page, $t_offset ); - $t_row_count = db_num_rows( $t_result ); $t_id_array_lastmod = array(); - for( $i = 0;$i < $t_row_count;$i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { $t_id_array_lastmod[] = (int)$t_row['id']; $t_rows[] = $t_row; } @@ -2082,8 +2080,7 @@ function filter_cache_result( array $p_rows, array $p_id_array_lastmod ) { # perform query $t_result = db_query( $t_query ); $t_row_count = db_num_rows( $t_result ); - for( $i = 0;$i < $t_row_count;$i++ ) { - $t_row = db_fetch_array( $t_result ); + while ( $t_row = db_fetch_array( $t_result ) ) { $t_stats[$t_row['bug_id']] = $t_row; } @@ -4496,9 +4493,8 @@ function filter_db_set_for_current_user( $p_project_id, $p_is_public, $p_name, $ AND name=' . db_param(); $t_result = db_query( $t_query, array( $t_user_id, $c_project_id, $p_name ) ); - if( db_num_rows( $t_result ) > 0 ) { - $t_row = db_fetch_array( $t_result ); - + $t_row = db_fetch_array( $t_result ); + if( $t_row ) { $t_query = 'UPDATE {filters} SET is_public=' . db_param() . ', filter_string=' . db_param() . ' @@ -4521,8 +4517,7 @@ function filter_db_set_for_current_user( $p_project_id, $p_is_public, $p_name, $ AND name=' . db_param(); $t_result = db_query( $t_query, array( $t_user_id, $c_project_id, $p_name ) ); - if( db_num_rows( $t_result ) > 0 ) { - $t_row = db_fetch_array( $t_result ); + if( $t_row = db_fetch_array( $t_result ) ) { return $t_row['id']; } @@ -4557,9 +4552,7 @@ function filter_db_get_filter( $p_filter_id, $p_user_id = null ) { $t_query = 'SELECT * FROM {filters} WHERE id=' . db_param(); $t_result = db_query( $t_query, array( $c_filter_id ) ); - if( db_num_rows( $t_result ) > 0 ) { - $t_row = db_fetch_array( $t_result ); - + if( $t_row = db_fetch_array( $t_result ) ) { if( $t_row['user_id'] != $t_user_id ) { if( $t_row['is_public'] != true ) { return null; @@ -4603,8 +4596,7 @@ function filter_db_get_project_current( $p_project_id, $p_user_id = null ) { AND name=' . db_param(); $t_result = db_query( $t_query, array( $c_user_id, $c_project_id, '' ) ); - if( db_num_rows( $t_result ) > 0 ) { - $t_row = db_fetch_array( $t_result ); + if( $t_row = db_fetch_array( $t_result ) ) { return $t_row['id']; } @@ -4622,9 +4614,7 @@ function filter_db_get_name( $p_filter_id ) { $t_query = 'SELECT * FROM {filters} WHERE id=' . db_param(); $t_result = db_query( $t_query, array( $c_filter_id ) ); - if( db_num_rows( $t_result ) > 0 ) { - $t_row = db_fetch_array( $t_result ); - + if( $t_row = db_fetch_array( $t_result ) ) { if( $t_row['user_id'] != auth_get_current_user_id() ) { if( $t_row['is_public'] != true ) { return null; @@ -4659,7 +4649,7 @@ function filter_db_can_delete_filter( $p_filter_id ) { $t_result = db_query( $t_query, array( $c_filter_id, $t_user_id, -1 ) ); - if( db_num_rows( $t_result ) > 0 ) { + if( db_result( $result ) > 0 ) { return true; } @@ -4733,10 +4723,8 @@ function filter_db_get_available_queries( $p_project_id = null, $p_user_id = nul OR user_id = ' . db_param() . ') ORDER BY is_public DESC, name ASC'; $t_result = db_query( $t_query, array( $t_project_id, db_prepare_bool( true ), $t_user_id ) ); - $t_query_count = db_num_rows( $t_result ); - for( $i = 0; $i < $t_query_count; $i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { $t_overall_query_arr[$t_row['id']] = $t_row['name']; } diff --git a/core/print_api.php b/core/print_api.php index 398e964bf7..7d1c652355 100644 --- a/core/print_api.php +++ b/core/print_api.php @@ -413,12 +413,10 @@ function print_news_item_option_list() { WHERE project_id=' . db_param() . ' ORDER BY date_posted DESC'; } - $t_result = db_query( $t_query, ($t_global == true ? array() : array( $t_project_id ) ) ); - $t_news_count = db_num_rows( $t_result ); - for( $i = 0;$i < $t_news_count;$i++ ) { - $t_row = db_fetch_array( $t_result ); + $t_result = db_query( $t_query, ($t_global == true ? array() : array( $t_project_id ) ) ); + while( $t_row = db_fetch_array( $t_result ) ) { $t_headline = string_display( $t_row['headline'] ); $t_announcement = $t_row['announcement']; $t_view_state = $t_row['view_state']; @@ -873,10 +871,8 @@ function print_build_option_list( $p_build = '' ) { WHERE ' . $t_project_where . ' ORDER BY build DESC'; $t_result = db_query( $t_query ); - $t_option_count = db_num_rows( $t_result ); - for( $i = 0;$i < $t_option_count;$i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { $t_overall_build_arr[] = $t_row['build']; } @@ -1084,8 +1080,7 @@ function print_project_user_list_option_list2( $p_user_id ) { ORDER BY p.name'; $t_result = db_query( $t_query, array( (int)$p_user_id, true ) ); $t_category_count = db_num_rows( $t_result ); - for( $i = 0;$i < $t_category_count;$i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { $t_project_name = string_attribute( $t_row['name'] ); $t_user_id = $t_row['id']; echo ''; diff --git a/core/project_api.php b/core/project_api.php index 0463be7309..51548f3566 100644 --- a/core/project_api.php +++ b/core/project_api.php @@ -169,10 +169,8 @@ function project_cache_all() { if( !$g_cache_project_all ) { $t_query = 'SELECT * FROM {project}'; $t_result = db_query( $t_query ); - $t_count = db_num_rows( $t_result ); - for( $i = 0;$i < $t_count;$i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { $g_cache_project[(int)$t_row['id']] = $t_row; } @@ -482,10 +480,11 @@ function project_get_id_by_name( $p_project_name ) { $t_query = 'SELECT id FROM {project} WHERE name = ' . db_param(); $t_result = db_query( $t_query, array( $p_project_name ), 1 ); - if( db_num_rows( $t_result ) == 0 ) { - return 0; + $t_id = db_result( $t_result ); + if( $t_id ) { + return $t_id; } else { - return db_result( $t_result ); + return 0; } } @@ -561,8 +560,9 @@ function project_get_local_user_access_level( $p_project_id, $p_user_id ) { WHERE user_id=' . db_param() . ' AND project_id=' . db_param(); $t_result = db_query( $t_query, array( (int)$p_user_id, $p_project_id ) ); - if( db_num_rows( $t_result ) > 0 ) { - return (int)db_result( $t_result ); + $t_level = db_result( $t_result ); + if( $t_level ) { + return (int)$t_level; } else { return false; } @@ -582,8 +582,8 @@ function project_get_local_user_rows( $p_project_id ) { $t_user_rows = array(); $t_row_count = db_num_rows( $t_result ); - for( $i = 0;$i < $t_row_count;$i++ ) { - array_push( $t_user_rows, db_fetch_array( $t_result ) ); + while( $t_row = db_fetch_array( $t_result ) ) { + array_push( $t_user_rows, $t_row ); } return $t_user_rows; @@ -671,9 +671,7 @@ function project_get_all_user_rows( $p_project_id = ALL_PROJECTS, $p_access_leve AND access_level ' . $t_global_access_clause; $t_result = db_query( $t_query, array( $t_on ) ); - $t_row_count = db_num_rows( $t_result ); - for( $i = 0;$i < $t_row_count;$i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { $t_users[$t_row['id']] = $t_row; } } @@ -687,9 +685,8 @@ function project_get_all_user_rows( $p_project_id = ALL_PROJECTS, $p_access_leve AND l.project_id = ' . db_param(); $t_result = db_query( $t_query, array( $t_on, $c_project_id ) ); - $t_row_count = db_num_rows( $t_result ); - for( $i = 0; $i < $t_row_count; $i++ ) { - $t_row = db_fetch_array( $t_result ); + + while( $t_row = db_fetch_array( $t_result ) ) { if( is_array( $p_access_level ) ) { $t_keep = in_array( $t_row['access_level'], $p_access_level ); } else { diff --git a/core/sponsorship_api.php b/core/sponsorship_api.php index 010ef8dcb2..a54173a0fa 100644 --- a/core/sponsorship_api.php +++ b/core/sponsorship_api.php @@ -119,7 +119,9 @@ function sponsorship_cache_row( $p_sponsorship_id, $p_trigger_errors = true ) { $t_query = 'SELECT * FROM {sponsorship} WHERE id=' . db_param(); $t_result = db_query( $t_query, array( $c_sponsorship_id ) ); - if( 0 == db_num_rows( $t_result ) ) { + $t_row = db_fetch_array( $t_result ); + + if( !$t_row ) { $g_cache_sponsorships[$c_sponsorship_id] = false; if( $p_trigger_errors ) { @@ -130,7 +132,6 @@ function sponsorship_cache_row( $p_sponsorship_id, $p_trigger_errors = true ) { } } - $t_row = db_fetch_array( $t_result ); $g_cache_sponsorships[$c_sponsorship_id] = $t_row; return $t_row; @@ -178,12 +179,12 @@ function sponsorship_get_id( $p_bug_id, $p_user_id = null ) { $t_query = 'SELECT id FROM {sponsorship} WHERE bug_id=' . db_param() . ' AND user_id = ' . db_param(); $t_result = db_query( $t_query, array( (int)$p_bug_id, $c_user_id ), 1 ); - if( db_num_rows( $t_result ) == 0 ) { + $t_row = db_fetch_array( $t_result ); + + if( !$t_row ) { return false; } - $t_row = db_fetch_array( $t_result ); - return (integer)$t_row['id']; } diff --git a/core/tag_api.php b/core/tag_api.php index b4066bef64..ed5c442730 100644 --- a/core/tag_api.php +++ b/core/tag_api.php @@ -59,10 +59,10 @@ * @return boolean True if tag exists */ function tag_exists( $p_tag_id ) { - $t_query = 'SELECT * FROM {tag} WHERE id=' . db_param(); + $t_query = 'SELECT id FROM {tag} WHERE id=' . db_param(); $t_result = db_query( $t_query, array( $p_tag_id ) ); - return db_num_rows( $t_result ) > 0; + return ( db_result( $t_result ) !== false ); } /** diff --git a/core/user_api.php b/core/user_api.php index 3f8d0f8663..0cb5decea1 100644 --- a/core/user_api.php +++ b/core/user_api.php @@ -239,11 +239,7 @@ function user_is_name_unique( $p_username ) { $t_query = 'SELECT username FROM {user} WHERE username=' . db_param(); $t_result = db_query( $t_query, array( $p_username ), 1 ); - if( db_num_rows( $t_result ) > 0 ) { - return false; - } else { - return true; - } + return !db_result( $t_result ); } /** diff --git a/lost_pwd.php b/lost_pwd.php index 0a842ef9ca..bd36819710 100644 --- a/lost_pwd.php +++ b/lost_pwd.php @@ -74,8 +74,9 @@ # @todo Consider moving this query to user_api.php $t_query = 'SELECT id FROM {user} WHERE username = ' . db_param() . ' AND email = ' . db_param() . ' AND enabled=' . db_param(); $t_result = db_query( $t_query, array( $f_username, $f_email, true ) ); +$t_row = db_fetch_array( $t_result ); -if( 0 == db_num_rows( $t_result ) ) { +if( !$t_row ) { trigger_error( ERROR_LOST_PASSWORD_NOT_MATCHING_DATA, ERROR ); } @@ -83,7 +84,6 @@ trigger_error( ERROR_LOST_PASSWORD_NO_EMAIL_SPECIFIED, ERROR ); } -$t_row = db_fetch_array( $t_result ); $t_user_id = $t_row['id']; if( user_is_protected( $t_user_id ) ) { diff --git a/plugins/MantisGraph/core/graph_api.php b/plugins/MantisGraph/core/graph_api.php index df4e6d1ff8..1a9b1b03b8 100644 --- a/plugins/MantisGraph/core/graph_api.php +++ b/plugins/MantisGraph/core/graph_api.php @@ -700,12 +700,10 @@ function create_developer_summary() { $t_query = 'SELECT handler_id, status FROM {bug} WHERE handler_id > 0 ' . $t_specific_where; $t_result = db_query( $t_query ); - $t_total_handled = db_num_rows( $t_result ); $t_handler_arr = array(); $t_handlers = array(); - for( $i = 0;$i < $t_total_handled;$i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { if( !isset( $t_handler_arr[$t_row['handler_id']] ) ) { $t_handler_arr[$t_row['handler_id']]['res'] = 0; $t_handler_arr[$t_row['handler_id']]['open'] = 0; @@ -752,13 +750,10 @@ function create_reporter_summary() { $t_query = 'SELECT reporter_id FROM {bug} WHERE ' . $t_specific_where; $t_result = db_query( $t_query ); - $t_total_reported = db_num_rows( $t_result ); $t_reporter_arr = array(); $t_reporters = array(); - for( $i = 0;$i < $t_total_reported;$i++ ) { - $t_row = db_fetch_array( $t_result ); - + while( $t_row = db_fetch_array( $t_result ) ) { if( isset( $t_reporter_arr[$t_row['reporter_id']] ) ) { $t_reporter_arr[$t_row['reporter_id']]++; } else { @@ -797,8 +792,7 @@ function create_category_summary() { $t_category_count = db_num_rows( $t_result ); $t_metrics = array(); - for( $i = 0;$i < $t_category_count;$i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { $t_cat_name = $t_row['name']; $t_cat_id = $t_row['id']; $t_query = 'SELECT COUNT(*) FROM {bug} WHERE category_id=' . db_param() . ' AND ' . $t_specific_where; @@ -830,11 +824,8 @@ function create_cumulative_bydate() { # Get all the submitted dates $t_query = 'SELECT date_submitted FROM {bug} WHERE ' . $t_specific_where . ' ORDER BY date_submitted'; $t_result = db_query( $t_query ); - $t_bug_count = db_num_rows( $t_result ); - - for( $i = 0; $i < $t_bug_count; $i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { # rationalise the timestamp to a day to reduce the amount of data $t_date = $t_row['date_submitted']; $t_date = (int)( $t_date / SECONDS_PER_DAY ); @@ -863,8 +854,7 @@ function create_cumulative_bydate() { $t_last_id = 0; $t_last_date = 0; - for( $i = 0;$i < $t_bug_count;$i++ ) { - $t_row = db_fetch_array( $t_result ); + while( $t_row = db_fetch_array( $t_result ) ) { $t_id = $t_row['id']; # if h_last_updated is NULL, there were no appropriate history records diff --git a/print_all_bug_options_inc.php b/print_all_bug_options_inc.php index 9dd2a91146..87ae23e186 100644 --- a/print_all_bug_options_inc.php +++ b/print_all_bug_options_inc.php @@ -109,8 +109,10 @@ function edit_printing_prefs( $p_user_id = null, $p_error_if_protected = true, $ $t_query = 'SELECT print_pref FROM {user_print_pref} WHERE user_id=' . db_param(); $t_result = db_query( $t_query, array( $p_user_id ) ); + $t_row = db_fetch_array( $t_result ); + ## OOPS, No entry in the database yet. Lets make one - if( 0 == db_num_rows( $t_result ) ) { + if( !$t_row ) { # create a default array, same size than $t_field_name for( $i=0; $i<$t_field_name_count; $i++ ) { $t_default_arr[$i] = 1 ; @@ -128,10 +130,11 @@ function edit_printing_prefs( $p_user_id = null, $p_error_if_protected = true, $ # Rerun select query $t_query = 'SELECT print_pref FROM {user_print_pref} WHERE user_id=' . db_param(); $t_result = db_query( $t_query, array( $p_user_id ) ); + + $t_row = db_fetch_array( $t_result ); } # putting the query result into an array with the same size as $t_fields_arr - $t_row = db_fetch_array( $t_result ); $t_prefs = $t_row['print_pref']; # Account Preferences Form BEGIN