PHP ODBC 数据类型不匹配 (MS Access)

Posted

技术标签:

【中文标题】PHP ODBC 数据类型不匹配 (MS Access)【英文标题】:PHP ODBC Data Type Mismatch (MS Access) 【发布时间】:2018-05-17 09:22:54 【问题描述】:

当我尝试使用 PHP ODBCMS Access DB 查询时收到此错误消息。我知道这是一个重复的问题,但他们都没有解决方案。

我尝试过使用 SQL Fiddle 进行查询,它可以工作,但不知何故,在 php ODBC 中现在无法正常工作。我也想知道“15.00”显示为“15”而不是“15.00”是否正常?

这是链接 -> http://sqlfiddle.com/#!9/087d72/17

错误信息:

警告:odbc_exec():SQL 错误:[Microsoft][ODBC Microsoft Access Driver] 条件表达式中的数据类型不匹配。SQLExecDirect 中的 SQL 状态 22005

我在 MS Access DB 中的表(表名“tblPAyTrans”)

Employee ID | 1stHalf | 2ndHalf | Month | Year | EPFee |
   1011     |    1    |    0    |   2   | 2017 | 15.00 |
   1011     |    0    |    1    |   2   | 2017 | 29.00 |
   1011     |    0    |    1    |   3   | 2018 | 29.00 |

我想要的输出:

Employee ID|1stHalf|2ndHalf|Month|Year|EPFee(1sthalf)|EPFee(2ndHalf)|
   1011    |   1   |   1   |  2  |2017|     15.00    |    29.00     |
   1011    |   0   |   1   |  3  |2018|     29.00    |     0.00     |

这是我的代码:

<?php
  $conn=odbc_connect('payrolldb','','COMPLETEPAYROLL');

  if (!$conn)
  
    exit("Connection Failed: " . $conn);
  

  $id = 1001;
  $first_half = 1;
  $second_half = 1;

  $sql = "SELECT `Employee ID`,
         (SELECT `1stHalf` FROM `tblPAyTrans` WHERE `Employee ID` = '$id' AND `1stHalf` = '$first_half') AS `FirstHalf`,
         (SELECT `2ndHalf` FROM `tblPAyTrans` WHERE `Employee ID` = '$id'AND `2ndHalf` = '$second_half') AS `SecondHalf`,             
         (SELECT `EPFee` FROM `tblPAyTrans` WHERE `Employee ID` = '$id' AND `1stHalf` = '$first_half') AS `EPF1stHalf`, 
         (SELECT `EPFee` FROM `tblPAyTrans` WHERE `Employee ID` = '$id' AND `2ndHalf` = '$second_half') AS `EPF2ndHalf`,
         `Month`, `Year`
         FROM `tblPAyTrans`
         GROUP BY `Employee ID`,`Month`,`Year`";

  $rs=odbc_exec($conn,$sql);

  if (!$rs)
  
    exit("Error in SQL");
  

  echo "<br>RESULT: <br><br>";
  echo "<table border='2'>
          <tr>
            <th>Employee ID</th>
            <th>1st Half</th>
            <th>2nd Half</th>        
            <th>Month</th>
            <th>Year</th>
            <th>EPF1stHalf</th>
            <th>EPF2ndHalf</th>
          </tr>";

  while (odbc_fetch_row($rs))
  
    $EmployeeID = odbc_result($rs,'Employee ID');
    $FirstHalf = odbc_result($rs,'FirstHalf');
    $SecondHalf = odbc_result($rs,'SecondHalf');     
    $Month = odbc_result($rs,'Month');
    $Year = odbc_result($rs,'Year');
    $EPF1stHalf = $row['EPF1stHalf'];
    $EPF2ndHalf = $row['EPF2ndHalf'];

    echo "<tr>";
      echo "<td>" . $EmployeeID. "</td>";
      echo "<td>" . $FirstHalf. "</td>";
      echo "<td>" . $SecondHalf. "</td>";            
      echo "<td>" . $Month. "</td>";
      echo "<td>" . $Year. "</td>";
      echo "<td>" . $EPF1stHalf. "</td>";
      echo "<td>" . $EPF2ndHalf. "</td>";
    echo "</tr>"; 
  
  odbc_close($conn);
  echo "</table>";
?>

如果我做错了,请纠正我。我真的很感激。

根据古斯塔夫: 将语法更改为此也会产生相同的错误。

SELECT [Employee ID],
     (SELECT [1stHalf] FROM tblPAyTrans WHERE [Employee ID] = '$id' AND [1stHalf] = '$first_half') AS FirstHalf,
     (SELECT [2ndHalf] FROM tblPAyTrans WHERE [Employee ID] = '$id'AND [2ndHalf] = '$second_half') AS SecondHalf,             
     (SELECT [EPFee] FROM tblPAyTrans WHERE [Employee ID] = '$id' AND [1stHalf] = '$first_half') AS EPF1stHalf, 
     (SELECT [EPFee] FROM tblPAyTrans WHERE [Employee ID] = '$id' AND [2ndHalf] = '$second_half') AS EPF2ndHalf,
     [Month], [Year]
     FROM tblPAyTrans
     GROUP BY [Employee ID],[Month],[Year]

【问题讨论】:

【参考方案1】:

如果是Access SQL,修改为:

$sql = "SELECT [Employee ID],
       (SELECT [1stHalf] FROM tblPAyTrans … etc.

编辑:您可能还需要连接:

… WHERE [Employee ID] = $id AND [2ndHalf] = $second_half) AS [SecondHalf], …

或:

… WHERE [Employee ID] = " . $id . " AND [2ndHalf] = " . $second_half . ") AS [SecondHalf], …

如果值是文本,则需要引号:

… WHERE [Employee ID] = '$id' AND …

【讨论】:

以上是关于PHP ODBC 数据类型不匹配 (MS Access)的主要内容,如果未能解决你的问题,请参考以下文章

Excel ODBC SQL 语句抛出错误'[ODBC EXCEL 驱动程序] 条件中的数据类型不匹配

使用通过 ODBC 访问的条件表达式中的数据类型不匹配

使用 ODBC 连接时如何在 MS Access 或 SQL Server 中使用日期数据类型

PHP PDO ODBC - 等于运算符中的数据类型不兼容

连接到 ODBC 时出现 MS Access 错误

CodeIgniter框架连接读取MS Access数据库文件