错误:插入时SQLSTATE [HY093],但在代码中找不到错误

时间:2015-01-10 17:19:22

标签: php mysql

错误:SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配

让我们说我们正在寻找一行:

$row['ConvLog'] = '12/29/2014 2:44PM - working with the lender to remove the mortgage late so we are able to refinance the client.  - User 1 12/04/2014 2:27PM - file suspended until rapid rescore comes back removing late payment from credit.  - User 2 12/02/2014 2:25PM - Check stubs to Lorena. - User 3';

我已经使用下面的名称对我的数据库列名进行了三次检查,并且它们匹配。为什么我收到此错误?

脚本:

<?php 
$host = 'localhost';
$db_user = '';
$db_pass = '';
$db_name = '';
try {
  $pdo = new PDO('mysql:host='.$host.';dbname='.$db_name.'', $db_user, $db_pass);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $stmt = $pdo->prepare('SELECT * FROM test');
  $stmt->execute();

  $result = $stmt->fetchAll();

  if ( count($result) ) { 
    foreach($result as $row) {

            $string = $row['ConvLog'];
     // $column1 = $row[0]; just the ID, don't need to copy
      $column2 = $row[1];
      $column3 = $row[2];
      $column4 = $row[3];

?>

<?php

preg_match_all('/(.*)([0-9]{2}\/[0-9]{2}\/[0-9]{2,4})(.*)/', $string, $matches, PREG_SET_ORDER);

foreach ($matches as $val) {
    echo "matched: " . $val[0] . "<br>";
   /* echo "part 1: " . $val[1] . "<br>";
    echo "part 2: " . $val[2] . "<br>";
    echo "part 3: " . $val[3] . "<br>";
    echo "part 4: " . $val[4] . "<br><br>"; */

// start crazy shit

$db_user = '';
$db_pass = '';
try {
  $pdo = new PDO('mysql:host=localhost;dbname=', $db_user, $db_pass);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $pdo->prepare('INSERT INTO test4 VALUES(:id,:columnA,:columnB,:Processor,:Originator,:FOLDER,:Filename,:LoanAmount,:Rate,:LastName,:FirstName,:Subject Property Address,:City,:County,:CurrentStatus,:CurrentStartDate,:EstCloseDate,:CreditRpt,:Prospect,:PrePacking,:Application,:SenttoProc,:TitleOrdered,:TitleRecvd,:Submitted,:Approved,:RateLocked,:RateLockedStatus,:RateLock,:RateExp,:Suspended,:ReSubmitted,:CleartoClose,:DocsDrawn,:DocsOut,:Docs Recd,$column2:Closed,:Funded,:Recorded,:Denied,:Cancelled,:LOHold,:LoHoldField,:EscrowHold,:EscrowHoldField,:Loan Program,:CC Scenario,:Subject Property State,:Subject Property Zip Code,:Broker Fee,:Broker Pts,:Broker Flat,:Lender Name,:Lender Cpny Name,:Appraisal Rpt,:Late Basic Docs,:Late Miscellaneous,:Late VODs,:Late VOEs,:Late VOLs,:Late VOMs,:Outstanding Docs,:Outstanding Misc,:Outstanding Verifs,:Outstanding Conditions,:Purc,:Escrow Company,:Appraisal Ordered,:ConvLog,:Appraisal Required,:Suspended Reason,:LO Hold Reason,:Escrow Hold Reason,:Appraisal Ordered Status,:AppraisalOrderedField,:Subordination Ordered Status,:SubordinationOrderedField,:PreUnderwriting Status,:PreUnderwritingField
    )');
  $stmt->execute(array(
':id' => '', ':columnA' => $column2, ':columnB' => $column3, ':Processor' => 'test', ':Originator' => 'test', ':FOLDER' => 'test', ':Filename' => 'test', ':LoanAmount' => 'test', ':Rate' => 'test', ':LastName' => 'test', ':FirstName' => 'test', ':Subject Property Address' => 'test', ':City' => 'test', ':County' => 'test', ':CurrentStatus' => 'test', ':CurrentStartDate' => 'test', ':EstCloseDate' => 'test', ':CreditRpt' => 'test', ':Prospect' => 'test', ':PrePacking' => 'test', ':Application' => 'test', ':SenttoProc' => 'test', ':TitleOrdered' => 'test', ':TitleRecvd' => 'test', ':Submitted' => 'test', ':Approved' => 'test', ':RateLocked' => 'test', ':RateLockedStatus' => 'test', ':RateLock' => 'test', ':RateExp' => 'test', ':Suspended' => 'test', ':ReSubmitted' => 'test', ':CleartoClose' => 'test', ':DocsDrawn' => 'test', ':DocsOut' => 'test', ':Docs Recd' => 'test', ':Closed' => 'test', ':Funded' => 'test', ':Recorded' => 'test', ':Denied' => 'test', ':Cancelled' => 'test', ':LOHold' => 'test', ':LoHoldField' => 'test', ':EscrowHold' => 'test', ':EscrowHoldField' => 'test', ':Loan Program' => 'test', ':CC Scenario' => 'test', ':Subject Property State' => 'test', ':Subject Property Zip Code' => 'test', ':Broker Fee' => 'test', ':Broker Pts' => 'test', ':Broker Flat' => 'test', ':Lender Name' => 'test', ':Lender Cpny Name' => 'test', ':Appraisal Rpt' => 'test', ':Late Basic Docs' => 'test', ':Late Miscellaneous' => 'test', ':Late VODs' => 'test', ':Late VOEs' => 'test', ':Late VOLs' => 'test', ':Late VOMs' => 'test', ':Outstanding Docs' => 'test', ':Outstanding Misc' => 'test', ':Outstanding Verifs' => 'test', ':Outstanding Conditions' => 'test', ':Purc' => 'test', ':Escrow Company' => 'test', ':Appraisal Ordered' => 'test', ':ConvLog' => 'test', ':Appraisal Required' => 'test', ':Suspended Reason' => 'test', ':LO Hold Reason' => 'test', ':Escrow Hold Reason' => 'test', ':Appraisal Ordered Status' => 'test', ':AppraisalOrderedField' => 'test', ':Subordination Ordered Status' => 'test', ':SubordinationOrderedField' => 'test', ':PreUnderwriting Status' => 'test', ':PreUnderwritingField' => 'test'
  ));

} catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();
  }

// end crazy shit

}

?>

<?php      
    }   
  } else {
    echo "No rows returned.";
  }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
?>

这是一个只有5列的示例,但我仍然无法使用反引号。我用一个反引号,然后另一个和两个反复尝试。我做错了什么?

$stmt = $pdo->prepare('INSERT INTO test2 VALUES(:id,`:my number`,:blah,:cool,:notes)');
  $stmt->execute(array(
    ':id' => '', `:my number` => $column2, ':blah' => $column3, ':cool' => $column4, 'notes' => $val[0]
  ));

5列的更新示例:

$stmt = $pdo->prepare('INSERT INTO test2 (id, `my number`, blah, cool, notes) VALUES (?, ?, ?, ?, ?)');
  $stmt->execute(array('', 'test', 'test', 'test','test'));

1 个答案:

答案 0 :(得分:0)

:my number不是一个好的占位符名称,只需将:mynumber:my_number更改为INSERT INTO test2 VALUES(:id,:mynumber,:blah,:cool,:notes) 即可避免该空格。

':mynumber' => $column2.

然后绑定时(引号不是反引号):

{{1}}
  • 你唯一需要反击的是在列或表上而不是 占位符。
  • 占位符名称的规则是字母数字和下划线