' PDOException':带有消息' SQLSTATE [HY093]的参数号无效:绑定变量的数量与令牌的数量不匹配'

时间:2014-08-08 14:43:41

标签: php sql pdo

我的问题是我尝试连接到我的数据库(它有4个元素,ID,日期,时间和最后价值(last_value)),但我打算将我的数据输入数据库" message_id" element为null。任何人都可以帮助我吗?

堆栈跟踪

  

' PDOException':带有消息的参数号无效   ' SQLSTATE [HY093]:绑定变量的数量与数量不匹配   令牌'在   C:\瓦帕\ WWW \ CTEC3110 \包括\类\ cw_framework \ class.CourseWorkDatabaseWrapper.php:78   堆栈跟踪:#0   C:\瓦帕\ WWW \ CTEC3110 \包括\类\ cw_framework \ class.CourseWorkDatabaseWrapper.php(78):   PDOStatement-> execute()#1   C:\瓦帕\ WWW \ CTEC3110 \包括\类\ cw_download_message_data \ class.CourseWorkDownloadMessageDataModel.php(216):   CourseWorkDatabaseWrapper-> safe_query(' SELECT message _...',Array)#2   C:\瓦帕\ WWW \ CTEC3110 \包括\类\ cw_download_message_data \ class.CourseWorkDownloadMessageDataModel.php(169):   CourseWorkDownloadMessageDataModel-> do_check_if_data_exists()#3   C:\瓦帕\ WWW \ CTEC3110 \包括\类\ cw_framework \ class.CourseWorkContainer.php(126):   CourseWorkDownloadMessageDataModel-> do_store_downloaded_message_data()#4 C:\ wamp \ www \ CTEC3110 \ includes \ classes \ cw_download_message_data \ class.CourseWorkDownloadMessageDataController.php(6):   CourseWorkCont in   C:\瓦帕\ WWW \ CTEC3110 \包括\类\ cw_framework \ class.CourseWorkDatabaseWrapper.php   在第91行

检查数据是否存在代码

$m_message_id = NULL;
$m_message_date = $this->c_arr_download_message_data['message-date'];
$m_message_time = $this->c_arr_download_message_data['message-time'];
$m_message_last_value = $this->c_arr_download_message_data['message-data'];

$m_sql_query_string = CourseWorkSqlQuery::does_message_exist();

$m_arr_sql_query_parameters =
array(  ':message_id' => $m_message_id,
        ':message_date' => $m_message_date,
        ':message_time' => $m_message_time,
        ':message_last_value' => $m_message_last_value);

$this->c_obj_database_handle->safe_query($m_sql_query_string, $m_arr_sql_query_parameters);

SQL查询

public static function does_message_exist()
{
    $m_sql_query_string  = "SELECT message_id, message_time, message_date, message_last_value ";
    $m_sql_query_string .= "FROM cw_messages ";
    $m_sql_query_string .= "WHERE message_id = :message_id ";
    $m_sql_query_string .= "AND message_time = :message_time ";
    $m_sql_query_string .= "AND message_date = :message_date ";
    $m_sql_query_string .= "AND message_last_value = :message_last_value ";
    $m_sql_query_string .= "LIMIT 1";
    return $m_sql_query_string;
}

查询检查代码(在try / catch下)

$m_temp = array();
$this->c_obj_database_handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$this->c_obj_stmt = $this->c_obj_database_handle->prepare($m_query_string);

// bind the parameters
if (sizeof($m_arr_query_parameters) > 0)
{
    foreach ($m_arr_query_parameters as $m_param_key => $m_param_value)
    {
        $m_temp[$m_param_key] = $m_param_value;
        $this->c_obj_stmt->bindParam($m_param_key+1, $m_temp[$m_param_key], PDO::PARAM_STR);
    }
}

// execute the query
$m_execute_result = $this->c_obj_stmt->execute();
$this->c_arr_database_connection_messages['execute-OK'] = $m_execute_result;
$m_database_query_execute_error = false;

1 个答案:

答案 0 :(得分:1)

试试这个,因为你使用的是命名参数,所以将密钥递增1是没有意义的:

foreach ($m_arr_query_parameters as $m_param_key => $m_param_value)
{
    $this->c_obj_stmt->bindValue($m_param_key, $m_param_value);
}