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

时间:2018-05-17 09:22:54

标签: php sql ms-access odbc

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

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

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

错误讯息:

  

警告:odbc_exec():SQL错误:[Microsoft] [ODBC Microsoft Access驱动程序]条件表达式中的数据类型不匹配。,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 个答案:

答案 0 :(得分:2)

如果这是Access SQL,请将其修改为:

_R_CHECK_CRAN_INCOMING_

Check whether package is suitable for publication on CRAN. Default: false, except for CRAN submission checks.

_R_CHECK_CRAN_INCOMING_REMOTE_

Include checks that require remote access among the above. Default: same as _R_CHECK_CRAN_INCOMING_

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

$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], …