PDOException:带有消息的参数号无效' SQLSTATE [HY093]:列/参数是基于1的'

时间:2014-08-15 13:26:16

标签: php mysql

这个项目非常艰难。我正在尝试访问具有各种字段的数据库,每个字段都有一个唯一的变量;但我仍然得到“专栏是基于1的”

堆栈跟踪

Notice: exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: Columns/Parameters are 1-based' in C:\wamp\www\CTEC3110\includes\classes\cw_framework\class.CourseWorkDatabaseWrapper.php:80
Stack trace:
#0 C:\wamp\www\CTEC3110\includes\classes\cw_framework\class.CourseWorkDatabaseWrapper.php(80): PDOStatement->bindParam(0, '447950491677', 2)
#1 C:\wamp\www\CTEC3110\includes\classes\cw_download_message_data\class.CourseWorkDownloadMessageDataModel.php(357): CourseWorkDatabaseWrapper->safe_query('SELECT id FROM ...', Array)
#2 C:\wamp\www\CTEC3110\includes\classes\cw_download_message_data\class.CourseWorkDownloadMessageDataModel.php(181): CourseWorkDownloadMessageDataModel->do_check_if_data_exists()
#3 C:\wamp\www\CTEC3110\includes\classes\cw_framework\class.CourseWorkContainer.php(142): CourseWorkDownloadMessageDataModel->do_store_downloaded_message_data()
#4 C:\wamp\www\CTEC3110\includes\classes\cw_download_message_data\class.CourseWorkDownloadMessageDataController.php(15): CourseWorkContaine in C:\wamp\www\CTEC3110\includes\classes\cw_framework\class.CourseWorkDatabaseWrapper.php on line 102


$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_keys)
    {
        var_dump($m_param_keys);

        foreach ($m_param_keys as $m_param_key => $m_param_value)
        {
            $this->c_obj_stmt->bindParam($m_param_key, $m_param_value, 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;

其中“$ m_query_string”是:

SELECT id
FROM cw_messages
WHERE source = :source_terminal
    AND destination = :destination_terminal
    AND date = :message_date
    AND time = :message_time
    AND type = :message_type
    AND reference = :message_reference
    AND data = :message_data
LIMIT 1

以下是“$ m_arr_query_parameters”:

Array ( [:source_terminal] => Array ( [0] => 447950491677 [1] => 447950491677 ) [:destination_terminal] => Array ( [0] => 447817814149 [1] => 447817814149 ) [:message_date] => Array ( [0] => 14/08/2014 [1] => 14/08/2014 [2] => 2014-08-14 [3] => 2014-08-14 ) [:message_time] => Array ( [0] => 18:57:31 [1] => 19:48:28 ) [:message_type] => Array ( [0] => SMS [1] => SMS ) [:message_reference] => Array ( [0] => 0 [1] => 0 ) [:message_data] => Array ( [0] => Test 3 [1] => Test at 20:48 ) ) 

这是$ m_param_keys的var转储:

  

数组(大小= 2)     0 =>字符串'447950491677'(长度= 12)     1 =>字符串'447950491677'(长度= 12)

3 个答案:

答案 0 :(得分:1)

查看你的$ m_param_keys转储,你绑定2个参数,我猜你试图根据你发布的参数数组绑定其中的7个参数。

同时查看错误:注意:异常' PDOException'消息' SQLSTATE [HY093]:参数号无效

我几乎可以告诉你,如果绑定了你应该使用的所有参数(阵列中的所有参数),你就不会再出现错误了。

编辑,添加代码:

$params = array(  ':source_terminal' => $m_message_source,
            ':destination_terminal' => $m_message_destination,
            ':message_date' => $m_message_date,
            ':message_time' => $m_message_time,
            ':message_type' => $m_message_type,
            ':message_reference' => $m_message_reference,
            ':message_data' => $m_message_data);

foreach( $params as $key => $val )
    $this->c_obj_stmt->bindParam($key, $val, PDO::PARAM_STR);

答案 1 :(得分:1)

您的转储显示$ m_param_keys是数字键控数组。你实际上是在尝试做

->bindParam(0, 447950491677)

PDO参数从1编号,而不是从0编号。您还使用 NAMED 参数,因此也不允许使用数字偏移进行绑定。绑定调用应该是

->bindParam(':source_terminal', $m_message_source)

答案 2 :(得分:1)

马克B的答案很接近,但最后,修复实际上比我想的更简单。

我正在通过一个太多的foreach循环传递$m_param_key,并且需要将它保持在外部循环上。

这是已解决的代码:

// bind the parameters
if (sizeof($m_arr_query_parameters) > 0)
{   
  foreach ($m_arr_query_parameters as $m_param_key => $m_param_values)
    {
      foreach($m_param_values as $m_param_value)
        {
          $this->c_obj_stmt->bindParam($m_param_key, $m_param_value);
        }
    }
}

// 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;