首頁 > 資料庫 > mysql教程 > 為什麼我在準備好的語句中收到「mysqli_stmt::bind_param(): 字元計數和綁定變數不符」錯誤?

為什麼我在準備好的語句中收到「mysqli_stmt::bind_param(): 字元計數和綁定變數不符」錯誤?

Mary-Kate Olsen
發布: 2024-11-21 17:20:11
原創
376 人瀏覽過

Why am I getting the

mysqli_stmt::bind_param(): 字元計數和綁定變數不符

當嘗試使用mysqli 預先準備語句將資料插入資料庫時,您可能會遇到以下錯誤「類型定義字串中的元素數量與綁定變數的數量不符。」當SQL 查詢中的佔位符標記(?)數量與傳遞給 bind_param 的類型定義字串中的字元數不符時,就會出現此錯誤。

在提供的範例中,有 65 個佔位符查詢中的標記和傳遞給 bind_param 的 65 個變數。但是,程式碼片段中沒有指定類型定義字串,因此無法驗證字元數是否與綁定變數的數量相符。

類型定義字串必須包含與佔位符相同的字元數在SQL查詢中,其中每個字元代表對應佔位符的資料類型。例如,在具有三個佔位符的 SQL 查詢中,bind_param 需要具有三個字元的類型定義字串,例如“sss”,以指示所有三個佔位符都是字串。

要解決該錯誤,請確保:

  • SQL 查詢包含正確數量的佔位符標記。
  • 提供給 bind_param 的型別定義字串與佔位符標記的數量,並為每個佔位符指定正確的資料型別。

以下是包含型別定義字串的修正範例:

// 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中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板