mysqli プリペアド ステートメントを使用してデータベースにデータを挿入しようとすると、次のエラーが発生する場合があります「型定義文字列内の要素の数がバインド変数の数と一致しません。」このエラーは、SQL クエリ内のプレースホルダー マーカー (?) の数と、bind_param に渡される型定義文字列内の文字数との間に不一致がある場合に発生します。
この例では、プレースホルダーは 65 個あります。クエリ内のマーカーと、bind_param に渡される 65 個の変数。ただし、型定義文字列はコード スニペットで指定されていないため、文字数がバインド変数の数と一致するかどうかを検証することはできません。
型定義文字列には、プレースホルダーと同じ数の文字が含まれている必要があります。 SQL クエリでは、各文字は対応するプレースホルダーのデータ型を表します。たとえば、3 つのプレースホルダーを含む SQL クエリでは、bind_param には、3 つのプレースホルダーすべてが文字列であることを示す「sss」などの 3 文字の型定義文字列が必要です。
エラーを解決するには、次のことを確認してください。
型定義文字列が含まれる修正された例を次に示します。
// Preparing our query statement via mysqli which will auto-escape all bad characters to prevent injection $query3 = 'INSERT INTO datashep_AMS.COMPLETE_APPLICATIONS ( project_name, status, funding_requested, project_title, program, county, parish, name_of_watercourse, which_is_a_tributary_of, name_of_applicant, contact_person_or_project_supervisor, relationship_to_organization, business_phone, home_phone, email, signature_of_thesis_or_study_supervisor, mailing_address, postal_code, website, mailing_address_for_payment, hst_registration_no, total_cost_dollar, total_cost_percent, dollar_amount_requested_from_nbwtf, percent_amount_requested_from_nbwtf, descriptive_summary, background_of_organization, mandate, years_in_existence, membership, accomplishments, previous_project_name, previous_project_number, previous_project_amount_received_from_nbwtf, summary_of_activities, summary_of_Results, project_title_2, reason_and_or_purpose, objectives, project_description, methods, equipment_and_materials_required, personnel_required, proposed_start_date, proposed_end_date, type_of_data_to_be_stored, where_will_it_be_housed, monitoring, short_term_achievement, long_term_achievement, previous_studies, required_permits, consultants, short_term_commitment, long_term_commitment, project_duration, project_evaluation, promotion_of_project, promotion_of_client, publication_of_results, community_benefits, effects_on_traditional_uses, possible_changes_in_public_access_to_areas, possible_impact_on_wildlife_and_or_environment, likelihood_of_future_requests_for_funding, list_all_other_funding_sources_for_this_project ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )'; // "Preparing" the query using mysqli->prepare(query) -- which is the equivalent of mysql_real_escape_string -- in other words, it's the SAFE database injection method $stmt = $dbConnection->prepare($query3); // "Bind_param" == replace all the "?"'s in the aforementioned query with the variables below $stmt->bind_param( "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss", $project_name, $status, $funding_requested, $project_title, $program, $county, $parish, $name_of_watercourse, $which_is_a_tributary_of, $name_of_applicant, $contact_person_or_project_supervisor, $relationship_to_organization, $business_phone, $home_phone, $email, $signature_of_thesis_or_study_supervisor, $mailing_address, $postal_code, $website, $mailing_address_for_payment, $hst_registration_no, $total_cost_dollar, $total_cost_percent, $dollar_amount_requested_from_nbwtf, $percent_amount_requested_from_nbwtf, $descriptive_summary, $background_of_organization, $mandate, $years_in_existence, $membership, $accomplishments, $previous_project_name, $previous_project_number, $previous_project_amount_received_from_nbwtf, $summary_of_activities, $summary_of_Results, $project_title_2, $reason_and_or_purpose, $objectives, $project_description, $methods, $equipment_and_materials_required, $personnel_required, $proposed_start_date, $proposed_end_date, $type_of_data_to_be_stored, $where_will_it_be_housed, $monitoring, $short_term_commitment, $long_term_achievement, $previous_studies, $required_permits, $consultants, $short_term_commitment, $long_term_commitment, $project_duration, $project_evaluation, $promotion_of_project, $promotion_of_client, $publication_of_results, $community_benefits, $effects_on_traditional_uses, $possible_changes_in_public_access_to_areas, $possible_impact_on_wildlife_and_or_environment, $likelihood_of_future_requests_for_funding, $list_all_other_funding_sources_for_this_project ); // Perform the actual query! $stmt->execute();
以上がプリペアドステートメントで「mysqli_stmt::bind_param(): 文字数とバインド変数の不一致」エラーが発生するのはなぜですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。